#include <bits/stdc++.h>
using namespace std;
int abss(int x) {
  if (x < 0) return -x;
}
int main() {
  int n;
  cin >> n;
  vector<int> both, a, b, none;
  for (int i = 0; i < n; i++) {
    string k;
    cin >> k;
    int l;
    cin >> l;
    if (k == "11") both.push_back(l);
    if (k == "10") a.push_back(l);
    if (k == "00") none.push_back(l);
    if (k == "01") b.push_back(l);
  }
  sort(both.begin(), both.end());
  sort(b.begin(), b.end());
  sort(a.begin(), a.end());
  sort(none.begin(), none.end());
  reverse(both.begin(), both.end());
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  reverse(none.begin(), none.end());
  int ans = 0;
  for (int i = 0; i < both.size(); i++) {
    ans += both[i];
  }
  vector<int> temp;
  if (a.size() > b.size()) {
    temp = a;
    a = b;
    b = temp;
  }
  for (int i = 0; i < a.size(); i++) {
    ans += a[i] + b[i];
  }
  for (int i = a.size(); i < b.size(); i++) {
    none.push_back(b[i]);
  }
  int cursize = 2 * a.size() + both.size();
  sort(none.begin(), none.end());
  reverse(none.begin(), none.end());
  for (int i = 0; i < min(both.size(), none.size()); i++) {
    ans += none[i];
  }
  cout << ans << endl;
}
