#include <bits/stdc++.h>
using namespace std;
const int MX = 200000;
vector<int> A, B, N;
long long n, sm, m;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s >> x;
    if (s == "11") {
      sm += x;
      m++;
    }
    if (s == "10") {
      A.push_back(x);
    }
    if (s == "01") {
      B.push_back(x);
    }
    if (s == "00") {
      N.push_back(x);
    }
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  int u = min(A.size(), B.size());
  for (int i = 0, j = A.size() - 1; i < u; i++, j--) {
    sm += A[j];
    A.pop_back();
  }
  for (int i = 0, j = B.size() - 1; i < u; i++, j--) {
    sm += B[j];
    B.pop_back();
  }
  for (int i = 0; i < A.size(); i++) N.push_back(A[i]);
  for (int i = 0; i < B.size(); i++) N.push_back(B[i]);
  sort(N.begin(), N.end());
  for (int i = 0, j = N.size() - 1; i < m && j >= 0; i++, j--) {
    sm += N[j];
  }
  cout << sm;
  return 0;
}
