#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> B, C, D;
  long long int best = 0;
  int a = 0;
  for (int i = 0; i < int(n); i++) {
    string s;
    cin >> s;
    long long v;
    cin >> v;
    if (s == "11") {
      best += v;
      a++;
    } else if (s == "01") {
      B.push_back(v);
    } else if (s == "10") {
      C.push_back(v);
    } else {
      D.push_back(v);
    }
  }
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  int pongo = a;
  int ponb = 0;
  for (int i = 0; i < int(min(B.size(), C.size())); i++) {
    best += (B[B.size() - i - 1] + C[C.size() - i - 1]);
    ponb++;
    pongo += 2;
  }
  for (int i = int(min(B.size(), C.size())); i < int(max(B.size(), C.size()));
       i++) {
    if (i < B.size()) {
      D.push_back(B[B.size() - 1 - i]);
    } else {
      D.push_back(C[C.size() - 1 - i]);
    }
  }
  sort(D.begin(), D.end());
  int ind = D.size() - 1;
  while (2 * (a + ponb) > pongo && ind >= 0) {
    pongo++;
    best += D[ind];
    ind--;
  }
  cout << best << endl;
}
