#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<int> Aonly;
  vector<int> Bonly;
  vector<int> both;
  vector<int> extra;
  string s;
  int x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s >> x;
    if (s == "11") both.push_back(x);
    if (s == "01") Bonly.push_back(x);
    if (s == "10") Aonly.push_back(x);
    if (s == "00") extra.push_back(x);
  }
  int sum = 0;
  int s1 = (int)both.size();
  int s2 = (int)Bonly.size();
  int s3 = (int)Aonly.size();
  for (int i = 0; i < s1; i++) sum += both[i];
  int s4 = min(s2, s3);
  sort(Aonly.begin(), Aonly.end(), greater<int>());
  sort(Bonly.begin(), Bonly.end(), greater<int>());
  for (int i = 0; i < s4; i++) {
    sum += Aonly[i];
    sum += Bonly[i];
  }
  for (int j = s4; j < s2; j++) extra.push_back(Bonly[j]);
  for (int j = s4; j < s3; j++) extra.push_back(Aonly[j]);
  sort(extra.begin(), extra.end(), greater<int>());
  int k = 0;
  for (int i = 0; i < extra.size(); i++) {
    if (k == s1) break;
    sum += extra[i];
    k++;
  }
  cout << sum << endl;
}
