#include <bits/stdc++.h>
using namespace std;
int a[400000 + 10], b[400000 + 10], c[400000 + 10], d[400000 + 10];
long long anum = 0, bnum = 0, cnum = 0, dnum = 0;
bool cmp(long long x, long long y) { return x > y; }
int main() {
  long long n;
  long long num = 0, supa = 0, supb = 0, value = 0, bcptr = 0, aptr = 0,
            power = 0, les;
  char statusone, statustwo;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> statusone >> statustwo >> power;
    if (statusone == '0' && statustwo == '0')
      a[anum++] = power;
    else if (statusone == '0' && statustwo == '1')
      b[bnum++] = power;
    else if (statusone == '1' && statustwo == '0')
      c[cnum++] = power;
    else if (statusone == '1' && statustwo == '1')
      d[dnum++] = power;
  }
  for (int i = 0; i < dnum; i++) {
    value += d[i];
  }
  num += dnum;
  supa += dnum;
  supb += dnum;
  sort(a, a + anum, cmp);
  sort(b, b + bnum, cmp);
  sort(c, c + cnum, cmp);
  les = min(bnum, cnum);
  for (int i = 0; i < les; i++) {
    value += b[i] + c[i];
  }
  if (bnum > cnum) {
    for (int i = cnum; i < bnum; i++) {
      a[anum++] = b[i];
    }
  } else if (bnum < cnum) {
    for (int i = bnum; i < cnum; i++) {
      a[anum++] = c[i];
    }
  }
  sort(a, a + anum, cmp);
  for (int i = 0; i < dnum; i++) {
    value += a[i];
  }
  cout << value << endl;
  return 0;
}
