#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, v;
  cin >> n;
  vector<int> both, a, b, none, s, l;
  string sx;
  for (int i = 0; i < n; i++) {
    cin >> sx;
    cin >> v;
    if (stoi(sx) == 11)
      both.push_back(v);
    else if (stoi(sx) == 10)
      a.push_back(v);
    else if (stoi(sx) == 1)
      b.push_back(v);
    else
      none.push_back(v);
  }
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  sort(none.rbegin(), none.rend());
  int ex = both.size(), nt = 0, tt;
  if (a.size() <= b.size()) {
    s = a;
    l = b;
  } else {
    s = b;
    l = a;
  }
  if ((int)l.size() < (int)s.size() + ex) {
    tt = (int)s.size() + ex - (int)l.size();
  } else {
    tt = 0;
  }
  int il = min((int)l.size() - 1, (int)s.size() + ex - tt - 1);
  while (ex - tt - nt > 0 && (tt + nt) < ((int)none.size()) && il >= 0) {
    if (l[il] < none[tt + nt]) {
      il--;
      nt++;
    } else {
      break;
    }
  }
  int ans = 0;
  for (int i = 0; i < both.size(); i++) ans += both[i];
  for (int i = 0; i < s.size(); i++) ans += s[i];
  for (int i = 0; i <= il; i++) ans += l[i];
  for (int i = 0; i < min((int)none.size(), nt + tt); i++) ans += none[i];
  cout << ans;
}
