#include <bits/stdc++.h>
using namespace std;
int n;
int a = 0, b = 0;
long long ans = 0;
vector<vector<int> > v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  v = vector<vector<int> >(n, vector<int>(4, 0));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v[i][1] = s[0] - '0';
    a += v[i][1];
    v[i][2] = s[1] - '0';
    b += v[i][2];
    cin >> v[i][0];
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int cant = min(a, b);
  int ca = 0, cb = 0;
  for (int i = 0; i < n; i++) {
    if (v[i][1] && v[i][2]) {
      v[i][3] = 1;
      ca++;
      cb++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (v[i][1] == 1 && ca < cant && !v[i][3]) {
      v[i][3] = 1;
      ca++;
    }
    if (v[i][2] == 1 && cb < cant && !v[i][3]) {
      v[i][3] = 1;
      cb++;
    }
  }
  int temp = 0;
  for (int i = 0; i < n; i++) {
    if (v[i][3] == 1) {
      ans += v[i][0];
      temp++;
    }
  }
  cant = (2 * cant) - temp;
  temp = 0;
  for (int i = 0; i < n; i++) {
    if (v[i][3] == 0 && temp < cant) {
      temp++;
      ans += v[i][0];
    }
  }
  cout << ans << endl;
  ;
}
