#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c;
long long res;
int main() {
  int n, cnt = 0;
  cin >> n;
  while (n--) {
    int k, v;
    cin >> k >> v;
    if (k == 11) {
      cnt++;
      res += v;
    }
    if (k == 10) a.push_back(v);
    if (k == 01) b.push_back(v);
    if (k == 00) c.push_back(v);
  }
  sort(a.begin(), a.end(), greater<int>());
  sort(b.begin(), b.end(), greater<int>());
  if (a.size() >= b.size()) {
    for (int i = 0; i < b.size(); ++i) res += a[i] + b[i];
    for (int i = b.size(); i < a.size(); ++i) c.push_back(a[i]);
  } else {
    for (int i = 0; i < a.size(); ++i) res += a[i] + b[i];
    for (int i = a.size(); i < b.size(); ++i) c.push_back(b[i]);
  }
  sort(c.begin(), c.end(), greater<int>());
  for (int i = 0; i < min((int)c.size(), cnt); ++i) res += c[i];
  cout << res;
}
