#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int MAXN = +3;
int n, cab, ans, sab;
vector<int> A, B, EMP;
void gg() {
  cout << 0;
  exit(0);
}
bool cmp(int a, int b) { return a > b; }
void init() {
  ios::sync_with_stdio(false);
  int typ, w;
  cin >> n;
  for (int i = 0; i < (int)(n); i++) {
    cin >> typ >> w;
    if (typ == 11) {
      cab++;
      ans += w;
      continue;
    } else if (typ == 10) {
      A.push_back(w);
    } else if (typ == 01) {
      B.push_back(w);
    } else {
      EMP.push_back(w);
    }
  }
  if (cab == 0 && (A.size() == 0 || B.size() == 0)) {
    gg();
  }
  sort((A).begin(), (A).end(), cmp);
  sort((B).begin(), (B).end(), cmp);
  sab = min(A.size(), B.size());
  for (int i = 0; i < (int)(sab); i++) {
    ans += A[i];
    ans += B[i];
  }
  A.erase(A.begin(), A.begin() + sab);
  B.erase(B.begin(), B.begin() + sab);
  for (int &i : A) {
    EMP.push_back(i);
  }
  for (int &i : B) {
    EMP.push_back(i);
  }
  sort((EMP).begin(), (EMP).end(), cmp);
  for (int i = 0; i < (int)(min(cab, (int)EMP.size())); i++) {
    ans += EMP[i];
  }
}
int main() {
  init();
  cout << ans;
  return 0;
}
