#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> ar[3];
  int su = 0, c = 0;
  for (int i = 0; i < n; i++) {
    string s1;
    int val;
    cin >> s1 >> val;
    if (s1 == "00") {
      ar[2].push_back(val);
    } else if (s1 == "01") {
      ar[1].push_back(val);
    } else if (s1 == "10") {
      ar[0].push_back(val);
    } else {
      su += val;
      c++;
    }
  }
  for (int i = 0; i < 2; i++) {
    sort(ar[i].begin(), ar[i].end(), [](int a, int b) { return a > b; });
  }
  bool fl = ar[0].size() > ar[1].size();
  for (int i = 0; i < ar[fl].size(); i++) {
    su += ar[0][i];
    su += ar[1][i];
  }
  vector<int> vec;
  for (int i = ar[fl].size(); i < ar[!fl].size(); i++) {
    vec.push_back(ar[!fl][i]);
  }
  vec.insert(vec.end(), ar[2].begin(), ar[2].end());
  sort(vec.begin(), vec.end());
  for (auto itr = vec.rbegin(); itr != vec.rbegin() + c && itr != vec.rend();
       itr++) {
    su += *itr;
  }
  cout << su << "\n";
}
