#include <bits/stdc++.h>
using namespace std;
int all[500050], a[500050], b[500050], alln[500050];
bool cmp(int a, int b) { return a > b; }
int main() {
  char ch[5];
  int n;
  int allnum = 0, anum = 0, bnum = 0, allnnum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int num;
    getchar();
    scanf("%s %d", ch, &num);
    if (ch[0] == '1') {
      if (ch[1] == '1') {
        all[allnum++] = num;
      } else {
        a[anum++] = num;
      }
    } else {
      if (ch[1] == '1') {
        b[bnum++] = num;
      } else {
        alln[allnnum++] = num;
      }
    }
  }
  long long ans = 0;
  sort(all, all + allnum, cmp);
  sort(a, a + anum, cmp);
  sort(b, b + bnum, cmp);
  for (int i = 0; i < allnum; i++) {
    ans += all[i];
  }
  int minn = min(anum, bnum);
  for (int i = 0; i < minn; i++) {
    ans += a[i] + b[i];
  }
  for (int i = minn; i < anum; i++) {
    alln[allnnum++] = a[i];
  }
  for (int i = minn; i < bnum; i++) {
    alln[allnnum++] = b[i];
  }
  sort(alln, alln + allnnum, cmp);
  for (int i = 0; i < allnum; i++) {
    ans += alln[i];
  }
  printf("%d\n", ans);
  return 0;
}
