#include <bits/stdc++.h>
using namespace std;
int n, a[4][400005], ans, cnt;
inline int read(int BASE = 10) {
  char ch = getchar();
  int ret = 0, f = 1;
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') ret = ret * BASE + ch - '0', ch = getchar();
  return ret * f;
}
bool cmp(int x, int y) { return x > y; }
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    int g = read(2);
    a[g][++a[g][0]] = read();
  }
  for (int i = 1; i <= a[3][0]; ++i) ans += a[3][i];
  cnt = a[3][0];
  sort(a[0] + 1, a[0] + 1 + a[0][0], cmp);
  sort(a[1] + 1, a[1] + 1 + a[1][0], cmp);
  sort(a[2] + 1, a[2] + 1 + a[2][0], cmp);
  int t1 = 1, t2 = 1, p1 = 1, p2, p;
  while (t1 <= a[1][0] && t2 <= a[2][0]) {
    ans += a[1][t1] + a[2][t2];
    ++t1, ++t2;
  }
  if (t1 > a[1][0])
    p2 = t2, p = 2;
  else
    p2 = t1, p = 1;
  for (int i = 1; i <= cnt && (p1 <= a[0][0] || p2 <= a[p][0]); ++i) {
    if (p1 > a[0][0])
      ans += a[p][p2++];
    else if (p2 > a[p][0])
      ans += a[0][p1++];
    else if (a[p][p2] > a[0][p1])
      ans += a[p][p2++];
    else
      ans += a[0][p1++];
  }
  printf("%d\n", ans);
  return 0;
}
