#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5 + 5;
int cnt[3];
void solve() {
  vector<long long> alice, bob, both, bad;
  int n;
  cin >> n;
  long long sum = 0;
  long long tt = 0;
  for (int i = 0; i < n; i++) {
    string s;
    long long x;
    cin >> s >> x;
    if (s == "00") bad.push_back(x);
    if (s == "01") bob.push_back(x);
    if (s == "10") alice.push_back(x);
    if (s == "11") sum += x, tt++, cnt[0]++, cnt[1]++;
  }
  sort(bob.begin(), bob.end());
  sort(alice.begin(), alice.end());
  sort(bad.begin(), bad.end());
  while (bob.size() && alice.size()) {
    cnt[0]++;
    cnt[1]++;
    tt += 2;
    sum += (bob.back() + alice.back());
    bob.pop_back();
    alice.pop_back();
  }
  vector<long long> tmp;
  int p = 0;
  if (bob.size())
    tmp = bob, p = 0;
  else if (alice.size())
    tmp = alice, p = 1;
  while (tmp.size() && bad.size()) {
    if (tmp.back() >= bad.back() && tt + 1 <= cnt[p] * 2 + 2 &&
        tt + 1 <= cnt[1 - p] * 2) {
      cnt[p]++;
      sum += tmp.back();
      tmp.pop_back();
      tt++;
    } else if (tt + 1 <= cnt[0] * 2 && tt + 1 <= cnt[1] * 2) {
      sum += bad.back();
      bad.pop_back();
      tt++;
    } else
      break;
  }
  while (tmp.size() && tt + 1 <= cnt[p] * 2 + 2 && cnt[1 - p] * 2 >= tt + 1) {
    sum += tmp.back();
    tmp.pop_back();
    tt++;
  }
  while (bad.size() && cnt[0] * 2 >= tt + 1 && cnt[1] * 2 >= tt + 1) {
    sum += bad.back();
    bad.pop_back();
    tt++;
  }
  cout << sum << '\n';
  return;
}
int main() {
  int T;
  T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
