#include <bits/stdc++.h>
using namespace std;
const int MAX = 4 * 1E5;
int main() {
  int n;
  scanf("%d", &n);
  long long ans = 0LL;
  vector<int> a, b, d;
  int c = 0;
  long long s1 = 0LL, s2 = 0LL;
  for (int i = 0; i < n; ++i) {
    int t, s;
    scanf("%d %d", &t, &s);
    if (t == 11) {
      ans += s;
      c++;
    } else {
      if (t == 10) {
        a.push_back(s);
        s1 += s;
      } else if (t == 01) {
        b.push_back(s);
        s2 += s;
      } else {
        d.push_back(s);
      }
    }
  }
  int rest = 0;
  if (a.size() < b.size()) {
    ans += s1;
    sort(b.rbegin(), b.rend());
    for (int i = 0; i < a.size(); ++i) {
      ans += b[i];
    }
    for (int i = a.size(); i < b.size(); ++i) {
      d.push_back(b[i]);
    }
  } else {
    ans += s2;
    sort(a.rbegin(), a.rend());
    for (int i = 0; i < b.size(); ++i) {
      ans += a[i];
    }
    for (int i = b.size(); i < a.size(); ++i) {
      d.push_back(a[i]);
    }
  }
  sort(d.rbegin(), d.rend());
  for (int i = 0; i < min((int)d.size(), c); ++i) {
    ans += d[i];
  }
  printf("%lld", ans);
  return 0;
}
