#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
long long typ[4], ali[N], bob[N], non[N], all[N];
struct a {
  long long v, t;
};
bool operator<(a x, a y) { return x.v < y.v; }
priority_queue<a> hp1;
char ss[4];
long long n, rd, c1, c2, c3, c4, cc1, cc2, peo, ans, b1, b2, mem;
int gkind(char *s) {
  if (s[0] == s[1] && s[0] == '0') return 0;
  if (s[0] == s[1] && s[0] == '1') return 3;
  if (s[0] == '0')
    return 1;
  else
    return 2;
}
bool cmp(long long a, long long b) { return a > b; }
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s%lld", ss, &rd);
    b1 |= (ss[0] - '0'), b2 |= (ss[1] - '0');
    int tmp = gkind(ss);
    typ[tmp]++;
    if (tmp == 2)
      ali[++c1] = rd;
    else if (tmp == 1)
      bob[++c2] = rd;
    else if (tmp == 3)
      ans += rd, cc1++, cc2++, peo++;
    else
      non[++c4] = rd;
  }
  if (!b1 || !b2) printf("0"), exit(0);
  int siz = min(c1, c2);
  sort(ali + 1, ali + 1 + c1, cmp);
  sort(bob + 1, bob + 1 + c2, cmp);
  for (int i = 1; i <= siz; i++) ans += ali[i] + bob[i], cc1++, cc2++, peo += 2;
  if (siz == c1)
    for (int i = siz + 1; i <= c2; i++) hp1.push((a){bob[i], 2});
  else
    for (int i = siz + 1; i <= c1; i++) hp1.push((a){ali[i], 1});
  for (int i = 1; i <= c4; i++) hp1.push((a){non[i], 3});
  while (!hp1.empty() && cc1 >= (peo + 1) / 2 && cc2 >= (peo + 1) / 2) {
    a tp = hp1.top();
    hp1.pop();
    ans += tp.v, mem = tp.v;
    if (tp.t == 1)
      cc1++, peo++;
    else if (tp.t == 2)
      cc2++, peo++;
    else
      peo++;
  }
  if (cc1 < (peo + 1) / 2 || cc2 < (peo + 1) / 2) ans -= mem;
  printf("%lld", ans);
  return 0;
}
