#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int n, a[4][N], cnt[4], now[4];
bool cmp(const int &AI, const int &BI) { return AI > BI; }
int main() {
  scanf("%d", &n);
  for (int i = (1), i_end_ = (n); i <= i_end_; ++i) {
    char str[5];
    scanf("%s", str + 1);
    int t = str[1] - '0' + (str[2] - '0') * 2;
    scanf("%d", &a[t][++cnt[t]]);
  }
  for (int i = (0), i_end_ = (3); i <= i_end_; ++i)
    sort(a[i] + 1, a[i] + cnt[i] + 1, cmp);
  int ans = 0, sl = 0, sr = 0, m = cnt[3];
  for (int i = (1), i_end_ = (cnt[3]); i <= i_end_; ++i) {
    ans += a[3][i];
    sl++, sr++;
  }
  int mx = min(cnt[1], cnt[2]);
  now[1] = now[2] = mx, m += 2 * mx;
  for (int i = (1), i_end_ = (mx); i <= i_end_; ++i) {
    ans += a[1][i];
    ans += a[2][i];
    sl++, sr++;
  }
  while (1) {
    if (sl * 2 < (m + 1) || sr * 2 < (m + 1)) break;
    m++;
    int t = 0;
    for (int i = (0), i_end_ = (2); i <= i_end_; ++i)
      if (now[i] + 1 <= cnt[i]) t = max(t, a[i][now[i] + 1]);
    for (int i = (0), i_end_ = (2); i <= i_end_; ++i)
      if (t == a[i][now[i] + 1]) {
        now[i]++;
        break;
      }
    ans += t;
  }
  printf("%d\n", ans);
  return 0;
}
