#include <bits/stdc++.h>
using namespace std;
int comp(int a, int b) { return a > b; }
int main() {
  vector<int> a, b, c, d;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int k, v;
    scanf("%d%d", &k, &v);
    if (k == 11)
      a.push_back(v);
    else if (k == 10)
      b.push_back(v);
    else if (k == 1)
      c.push_back(v);
    else if (k == 0)
      d.push_back(v);
  }
  int sum = 0;
  for (int i = 0; i < a.size(); i++) sum += a[i];
  sort(b.begin(), b.end(), comp);
  sort(c.begin(), c.end(), comp);
  sort(d.begin(), d.end(), comp);
  if (b.size() == c.size()) {
    for (int i = 0; i < b.size(); i++) sum = sum + b[i] + c[i];
    int len = min(a.size(), d.size());
    for (int i = 0; i < len; i++) sum = sum + d[i];
    printf("%d\n", sum);
  } else if (b.size() > c.size()) {
    for (int i = 0; i < c.size(); i++) sum = sum + b[i] + c[i];
    for (int i = c.size(); i < b.size(); i++) d.push_back(b[i]);
    sort(d.begin(), d.end(), comp);
    int len = min(a.size(), d.size());
    for (int i = 0; i < len; i++) sum = sum + d[i];
    printf("%d\n", sum);
  } else {
    for (int i = 0; i < b.size(); i++) sum = sum + b[i] + c[i];
    for (int i = b.size(); i < c.size(); i++) d.push_back(c[i]);
    sort(d.begin(), d.end(), comp);
    int len = min(a.size(), d.size());
    for (int i = 0; i < len; i++) sum = sum + d[i];
    printf("%d\n", sum);
  }
}
