#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
int a[N], b[N], c[N];
int main() {
  int n;
  cin >> n;
  int sa = 0, sb = 0, sc = 0, w = 0, re = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int q;
    cin >> q;
    if (s == "11")
      re += q, w++;
    else if (s == "10") {
      a[sa++] = q;
    } else if (s == "01") {
      b[sb++] = q;
    } else
      c[sc++] = q;
  }
  sort(a, a + sa);
  reverse(a, a + sa);
  sort(b, b + sb);
  reverse(b, b + sb);
  int q = min(sa, sb);
  for (int i = 0; i < q; i++) re += a[i];
  for (int i = 0; i < q; i++) re += b[i];
  for (int i = q; i < sa; i++) c[sc++] = a[i];
  for (int i = q; i < sb; i++) c[sc++] = b[i];
  sort(c, c + sc);
  reverse(c, c + sc);
  int mot = (2 * q) + w;
  q += w;
  for (int i = 0; i < sc; i++) {
    if ((mot + 1) <= (2 * q))
      re += c[i];
    else
      break;
    mot++;
  }
  cout << re << endl;
}
