#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    long long n, temp;
    long long ans = 0;
    string s;
    cin >> n;
    map<string, vector<long long> > m;
    for (long long int i = 0; i < n; i++) {
      cin >> s >> temp;
      m[s].push_back(temp);
    }
    for (auto it = (m).begin(); it != (m).end(); it++) {
      sort((m[it->first]).begin(), (m[it->first]).end(), greater<long long>());
    }
    long long t = 0;
    while (t < (long long int)m["01"].size() &&
           t < (long long int)m["10"].size()) {
      ans = ans + m["01"][t] + m["10"][t];
      t++;
    }
    long long ct = 0;
    long long t1 = t;
    long long t2 = t;
    long long t3 = 0;
    while (ct < (long long int)m["11"].size()) {
      long long v1 = 0, v2 = 0, v3 = 0, v4;
      ans += m["11"][ct];
      ct++;
      if (t1 < (long long int)m["01"].size()) v1 = m["01"][t1];
      if (t2 < (long long int)m["10"].size()) v2 = m["10"][t2];
      if (t3 < (long long int)m["00"].size()) v3 = m["00"][t3];
      long long d = max(v1, max(v2, v3));
      ans = ans + d;
      if (d == v1)
        t1++;
      else if (d == v2)
        t2++;
      else
        t3++;
    }
    cout << ans;
  }
  return 0;
}
