#include <bits/stdc++.h>
using namespace std;
const int32_t Accepted = 0;
template <typename T>
void in(T& x) {
  static char ch;
  static bool neg;
  for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar())
    ;
  for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar())
    ;
  x = neg ? -x : x;
}
template <typename T, typename... Types>
void in(T& x, Types&... oth) {
  static char ch;
  static bool neg;
  for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar())
    ;
  for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar())
    ;
  x = neg ? -x : x;
  in(oth...);
}
template <typename T>
T _pow_mod(T a, T b, T mod) {
  T base = a, res = 1;
  while (b) {
    if (b & 1) res = (res * base) % mod;
    base = (base * base) % mod;
    b >>= 1;
  }
  return res;
}
template <typename T>
T _pow(T a, T b) {
  T base = a, res = 1;
  while (b) {
    if (b & 1) res = (res * base);
    base = (base * base);
    b >>= 1;
  }
  return res;
}
template <typename T>
void out(T x) {
  char s[65];
  int32_t cnt = 0;
  if (x > 0)
    while (x > 0) s[cnt++] = (x - (x / 10 * 10)) + '0', x /= 10;
  else if (x < 0) {
    while (x < 0) s[cnt++] = (x / 10 * 10) - x + '0', x /= 10;
    s[cnt++] = '-';
  }
  if (cnt == 0)
    putchar('0');
  else
    for (int32_t i = cnt - 1; ~i; i--) putchar(s[i]);
}
template <typename T>
void print(T x) {
  out(x);
  putchar('\n');
}
template <typename T, typename... types>
void print(T x, types... oth) {
  out(x);
  putchar(' ');
  print(oth...);
}
const int32_t MAXN = 1e6 + 5;
int q[MAXN];
int w[MAXN];
int e[MAXN];
int r[MAXN];
bool cmp(int a, int b) { return a > b; }
int main() {
  int n;
  int t, a;
  int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
  in(n);
  priority_queue<int> u;
  for (int i = 0; i < n; i++) {
    in(t, a);
    if (t == 00) q[++cnt1] = a, u.push(a);
    if (t == 01) w[++cnt2] = a;
    if (t == 10) e[++cnt3] = a;
    if (t == 11) r[++cnt4] = a;
  }
  long long sum = 0;
  sort(w + 1, w + cnt2 + 1, cmp);
  sort(e + 1, e + cnt3 + 1, cmp);
  int A = 0, B = 0;
  int C = 0;
  for (int i = 1; i <= cnt4; i++) sum += r[i], A++, B++, C++;
  int limit = min(cnt2, cnt3);
  for (int i = 1; i <= limit; i++) sum += w[i] + e[i], A++, B++, C += 2;
  for (int i = limit + 1; i <= cnt2; i++) {
    u.push(w[i]);
  }
  for (int i = limit + 1; i <= cnt3; i++) {
    u.push(e[i]);
  }
  while (cnt4--) {
    if (u.empty()) break;
    sum += u.top();
    u.pop();
  }
  print(sum);
  return 0;
}
