#include <bits/stdc++.h>
using namespace std;
struct node {
  char s[3];
  int influence;
};
node a[400001];
int b[400001];
int n, i;
bool Cmpare(const node &a, const node &b) { return a.influence > b.influence; }
bool cmp(const int &a, const int &b) { return a > b; }
int main() {
  cin >> n;
  int a1 = 0, b1 = 0, m = 0, a0, b0;
  int p1 = 0, p2 = 0, p;
  long long count = 0;
  for (i = 0; i < n; i++) {
    cin >> a[a1].s >> a[a1].influence;
    if (a[a1].s[0] == '1' && a[a1].s[1] == '1') {
      count += a[a1].influence;
      m++;
    } else if (a[a1].s[0] == '0' && a[a1].s[1] == '0') {
      b[b1] = a[a1].influence;
      b1++;
    } else {
      if (a[a1].s[0] == '1')
        p1++;
      else
        p2++;
      a1++;
    }
  }
  if (p1 < p2)
    p = p1;
  else
    p = p2;
  p1 = 0;
  p2 = 0;
  sort(a, a + a1, Cmpare);
  for (i = 0; i < a1; i++) {
    if (a[i].s[0] == '1') {
      p1++;
      if (p1 <= p) {
        count += a[i].influence;
      } else {
        b[b1] = a[i].influence;
        b1++;
      }
    } else {
      p2++;
      if (p2 <= p) {
        count += a[i].influence;
      } else {
        b[b1] = a[i].influence;
        b1++;
      }
    }
  }
  sort(b, b + b1, cmp);
  for (i = 0; i < m; i++) {
    count += b[i];
  }
  cout << count << endl;
  return 0;
}
