#include <bits/stdc++.h>
using namespace std;
bool comp(int a, int b) { return a > b; }
int f[5][400005], cnt[5], sum = 0;
int main() {
  int n, d, i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> j;
    d = j / 5 + j % 10;
    cin >> j;
    f[d][cnt[d] + 1] = j;
    cnt[d]++;
  }
  for (i = 1; i <= cnt[3]; i++) {
    sum += f[3][i];
  }
  sort(f[1] + 1, f[1] + cnt[1] + 1, comp);
  sort(f[2] + 1, f[2] + cnt[2] + 1, comp);
  if (cnt[1] >= cnt[2]) {
    for (i = 1; i <= cnt[2]; i++) {
      sum += f[1][i] + f[2][i];
    }
    for (i = cnt[2] + 1; i <= cnt[1]; i++) {
      f[0][cnt[0] + i - cnt[2]] = f[1][i];
    }
    cnt[0] += cnt[1] - cnt[2];
    sort(f[0] + 1, f[0] + cnt[0] + 1, comp);
    for (i = 1; i <= min(cnt[0], cnt[3]); i++) {
      sum += f[0][i];
    }
    cout << sum;
  } else {
    for (i = 1; i <= cnt[1]; i++) {
      sum += f[1][i] + f[2][i];
    }
    for (i = cnt[1] + 1; i <= cnt[2]; i++) {
      f[0][cnt[0] + i - cnt[1]] = f[2][i];
    }
    cnt[0] += cnt[2] - cnt[1];
    sort(f[0] + 1, f[0] + cnt[0] + 1, comp);
    for (i = 1; i <= min(cnt[0], cnt[3]); i++) {
      sum += f[0][i];
    }
    cout << sum;
  }
}
