#include <bits/stdc++.h>
using namespace std;
int A[400005];
int B[400005];
int C[400005];
int D[400005];
bool cmp(int a, int b) { return a > b; }
int main() {
  int n;
  while (~scanf("%d", &n)) {
    int a = 0, b = 0, c = 0, d = 0;
    for (int i = 0; i < n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      if (x == 11) A[a++] = y;
      if (x == 10) B[b++] = y;
      if (x == 1) C[c++] = y;
      if (x == 0) D[d++] = y;
    }
    long long ans = 0;
    sort(A, A + a, cmp);
    sort(B, B + b, cmp);
    sort(C, C + c, cmp);
    for (int i = 0; i < a; i++) ans += (long long)A[i];
    for (int i = 0; i < min(b, c); i++)
      ans += (long long)B[i] + (long long)C[i];
    if (b > c) {
      for (int i = c; i < b; i++) D[d++] = B[i];
    } else {
      for (int i = b; i < c; i++) D[d++] = C[i];
    }
    sort(D, D + d, cmp);
    for (int i = 0; i < min(a, d); i++) ans += (long long)D[i];
    printf("%lld\n", ans);
  }
  return 0;
}
