#include <bits/stdc++.h>
using namespace std;
int n, ans, num_oo, num_oz, num_zo;
vector<int> zz, oz, zo;
bool cmp(int x, int y) { return x > y; }
int main() {
  cin >> n;
  for (int x, i = 1; i <= n; i++) {
    string s;
    cin >> s >> x;
    if (s == "11") {
      ans += x;
      num_oo++;
    }
    if (s == "00") zz.push_back(x);
    if (s == "10") oz.push_back(x);
    if (s == "01") zo.push_back(x);
  }
  sort(zz.begin(), zz.end(), cmp);
  sort(oz.begin(), oz.end(), cmp);
  sort(zo.begin(), zo.end(), cmp);
  vector<int> sum(zz.size(), 0);
  if (zz.size() > 0) {
    sum[0] = zz[0];
    for (int i = 1; i < sum.size(); i++) sum[i] = sum[i - 1] + zz[i];
  }
  int sz1 = zo.size(), sz2 = oz.size();
  vector<int> rem1;
  if (sz1 <= sz2) {
    for (int i = 0; i < sz1; i++) {
      ans += (zo[i] + oz[i]);
      num_oz++;
      num_zo++;
    }
    for (int i = sz1; i < sz2; i++) rem1.push_back(oz[i]);
  }
  if (sz1 > sz2) {
    for (int i = 0; i < sz2; i++) {
      ans += (zo[i] + oz[i]);
      num_oz++;
      num_zo++;
    }
    for (int i = sz2; i < sz1; i++) rem1.push_back(zo[i]);
  }
  vector<int> rem(rem1.size(), 0);
  if (rem1.size() > 0) {
    rem[0] = rem1[0];
    for (int i = 1; i < rem.size(); i++) rem[i] = rem[i - 1] + rem1[i];
  }
  int mx = 0;
  int a = num_oo + num_oz;
  int numb = num_oo + 2 * num_zo;
  int siz = rem.size();
  for (int xx = 0; xx < rem.size(); xx++)
    if (2 * a >= numb + xx + 1) {
      mx = max(mx, rem[xx]);
    }
  for (int i = 0; i < sum.size(); i++) {
    int m = num_oo + 2 * num_oz + i + 1;
    if (2 * a < m) break;
    int x = max(0, min(siz, num_oo - i - 1));
    if (rem.size() >= x && 2 * a >= m + x && x > 0) {
      mx = max(mx, rem[x - 1] + sum[i]);
    }
    if (2 * a >= m) mx = max(mx, sum[i]);
  }
  cout << ans + mx;
  return 0;
}
