#include <bits/stdc++.h>
using namespace std;
vector<long long> v[5];
int32_t main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  long long n, s = 0, alic = 0, bob = 0, tot = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    v[(a / 10) * 2 + (a % 10)].push_back(b);
  }
  for (long long i = 0; i < 4; i++) sort(v[i].begin(), v[i].end());
  for (long long i = 0; i < v[3].size(); i++)
    tot += 1, bob++, alic++, s += v[3][i];
  while (v[1].size() && v[2].size()) {
    s += v[1].back();
    s += v[2].back();
    v[1].pop_back();
    v[2].pop_back();
    bob++;
    alic++;
    tot += 2;
  }
  for (long long i = 0; i < v[1].size(); i++) v[0].push_back(v[1][i]);
  for (long long i = 0; i < v[2].size(); i++) v[0].push_back(v[2][i]);
  sort(v[0].begin(), v[0].end());
  while (v[0].size()) {
    if (tot + 1 <= 2 * alic && tot + 1 <= 2 * bob) {
      tot++;
      s += v[0].back();
      v[0].pop_back();
    } else
      break;
  }
  cout << s;
}
