#include <bits/stdc++.h>
using namespace std;
int n;
priority_queue<int> al, bob, all;
int main() {
  cin >> n;
  int sum = 0, nm = 0, nmal = 0;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s >> x;
    if (s[0] == '0' && s[1] == '0') {
      all.push(x);
    }
    if (s[0] == '1' && s[1] == '1') {
      sum += x;
      nm++;
      nmal += 1;
    }
    if (s[0] == '0' && s[1] == '1') {
      al.push(x);
    }
    if (s[0] == '1' && s[1] == '0') {
      bob.push(x);
    }
  }
  int nd = min(al.size(), bob.size());
  nm += nd;
  nmal += nd << 1;
  for (int i = 0; i < nd; i++) {
    sum += al.top();
    al.pop();
    sum += bob.top();
    bob.pop();
  }
  while (bob.size()) {
    all.push(bob.top());
    bob.pop();
  }
  while (al.size()) {
    all.push(al.top());
    al.pop();
  }
  int i = nm;
  while (i && all.size() && nm * 2 > nmal) {
    sum += all.top();
    all.pop();
    i--;
    nmal++;
  }
  cout << sum;
  return 0;
}
