#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int main() {
  int n;
  scanf("%d", &n);
  vector<int> alice, bob, zero;
  int ans = 0, cnt = 0, now = 0;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s >> x;
    if (s == "11") {
      ans += x;
      cnt++;
    } else if (s == "00") {
      zero.push_back(x);
    } else if (s == "10")
      alice.push_back(x);
    else if (s == "01")
      bob.push_back(x);
  }
  sort(alice.begin(), alice.end());
  sort(bob.begin(), bob.end());
  now = cnt;
  int mn = min(alice.size(), bob.size());
  for (int i = 0; i < mn; i++) {
    ans += bob.back();
    ans += alice.back();
    cnt++;
    now += 2;
    bob.pop_back();
    alice.pop_back();
  }
  while (alice.size()) {
    zero.push_back(alice.back());
    alice.pop_back();
  }
  while (bob.size()) {
    zero.push_back(bob.back());
    bob.pop_back();
  }
  sort(zero.begin(), zero.end());
  while (zero.size() && (cnt * 2) >= (now + 1)) {
    ans += zero.back();
    zero.pop_back();
    now++;
  }
  cout << ans;
  return 0;
}
