#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
long long a[maxn], b[maxn], c[maxn], d[maxn];
bool cmp(long long ab, long long bb) { return ab > bb; }
int main() {
  long long n;
  cin >> n;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  memset(d, 0, sizeof(d));
  long long x, y;
  long long q, w, e, r;
  q = w = e = r = 0;
  while (n--) {
    cin >> x >> y;
    if (x == 11)
      a[q++] = y;
    else if (x == 10)
      b[w++] = y;
    else if (x == 01)
      c[e++] = y;
    else if (x == 00)
      d[r++] = y;
  }
  long long ans = 0;
  long long i, j;
  for (i = 0; i < q; i++) ans += a[i];
  sort(b, b + w, cmp);
  sort(c, c + e, cmp);
  for (i = 0; i < min(w, e); i++) {
    ans += b[i];
    ans += c[i];
  }
  for (i = min(w, e); i < w; i++) d[r++] = b[i];
  for (i = min(w, e); i < e; i++) d[r++] = c[i];
  sort(d, d + r, cmp);
  for (i = 0; i < min(q, r); i++) {
    ans += d[i];
  }
  cout << ans << endl;
  return 0;
}
