#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c;
int main() {
  int n, x, coo = 0;
  long long ans = 0;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s >> x;
    if (s == "11") {
      ans += x;
      coo++;
    } else if (s == "00")
      a.push_back(x);
    else if (s == "01")
      b.push_back(x);
    else
      c.push_back(x);
  }
  sort(b.rbegin(), b.rend()), sort(c.rbegin(), c.rend());
  int mn = min(b.size(), c.size());
  for (int i = 0; i < mn; i++) ans += b[i] + c[i];
  if (b.size() != mn) {
    for (int i = b.size() - 1; i >= mn; i--) a.push_back(b[i]);
  }
  if (c.size() != mn) {
    for (int i = c.size() - 1; i >= mn; i--) a.push_back(c[i]);
  }
  sort(a.rbegin(), a.rend());
  int p = a.size();
  for (int i = 0; i < min(p, coo); i++) ans += a[i];
  cout << ans << endl;
  return 0;
}
