#include <bits/stdc++.h>
const int N = 400050;
int A[N], a, B[N], Sb[N], b, C[N], Sc[N], c, D[N], Sd[N], d;
int main() {
  int n, Sa = 0;
  scanf("%d", &n);
  while (n--) {
    int t, x;
    scanf("%d%d", &t, &x);
    if (t == 11)
      Sa += A[a++] = x;
    else if (t == 10)
      B[b++] = x;
    else if (t == 1)
      C[c++] = x;
    else
      D[d++] = x;
  }
  std::sort(A, A + a, std::greater<int>());
  std::sort(B, B + b, std::greater<int>());
  std::sort(C, C + c, std::greater<int>());
  std::sort(D, D + d, std::greater<int>());
  int ans = 0;
  for (int i = 0; i < b; ++i) Sb[i + 1] = Sb[i] + B[i];
  for (int i = 0; i < c; ++i) Sc[i + 1] = Sc[i] + C[i];
  for (int i = 0; i < d; ++i) Sd[i + 1] = Sd[i] + D[i];
  for (int cc = 0; cc <= c; ++cc) {
    int up = std::min(d, std::min(a + b - cc, a + cc - b));
    if (up < 0) continue;
    ans = std::max(ans, Sa + Sb[b] + Sc[cc] + Sd[up]);
  }
  for (int bb = 0; bb <= b; ++bb) {
    int up = std::min(d, std::min(a + bb - c, a + c - bb));
    if (up < 0) continue;
    ans = std::max(ans, Sa + Sb[bb] + Sc[c] + Sd[up]);
  }
  printf("%d\n", ans);
  return 0;
}
