#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  map<string, vector<long long> > mp;
  string s;
  long long val;
  long long sum = 0;
  long long c = 0;
  for (int i = 0; i < n; i++) {
    cin >> s;
    cin >> val;
    mp[s].push_back(val);
    if (s == "11") {
      sum += val;
      c++;
    }
  }
  sort(mp["01"].begin(), mp["01"].end());
  sort(mp["10"].begin(), mp["10"].end());
  long long si = min(mp["01"].size(), mp["10"].size());
  long long a = mp["01"].size();
  long long b = mp["10"].size();
  long long j = 0;
  for (long long i = a - 1; i >= 0; i--) {
    j++;
    if (j > si) break;
    sum += mp["01"][i];
    mp["01"].pop_back();
  }
  j = 0;
  for (long long i = b - 1; i >= 0; i--) {
    j++;
    if (j > si) break;
    sum += mp["10"][i];
    mp["10"].pop_back();
  }
  for (auto ele : mp["01"]) {
    mp["00"].push_back(ele);
  }
  for (auto ele : mp["10"]) {
    mp["00"].push_back(ele);
  }
  sort(mp["00"].begin(), mp["00"].end());
  reverse(mp["00"].begin(), mp["00"].end());
  si = mp["00"].size();
  for (auto ele : mp["00"]) {
    si--;
    c--;
    if (si < 0 || c < 0)
      break;
    else {
      sum += ele;
    }
  }
  cout << sum;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}
