#include <bits/stdc++.h>
using namespace std;
int partsum(vector<int> &v00, vector<int> &l, vector<int> &s) {
  sort(l.begin(), l.end(), std::greater<int>());
  int psum = accumulate(s.begin(), s.end(), 0);
  psum = accumulate(l.begin(), l.begin() + s.size(), psum);
  v00.insert(v00.end(), l.begin() + s.size(), l.end());
  sort(v00.begin(), v00.end(), std::greater<int>());
  return psum;
}
int main() {
  int n;
  cin >> n;
  vector<int> v11, v01, v10, v00;
  int type;
  int a;
  int total = 0;
  for (int i = 1; i <= n; i++) {
    cin >> type >> a;
    if (type == 11)
      v11.push_back(a);
    else if (type == 10)
      v10.push_back(a);
    else if (type == 01)
      v01.push_back(a);
    else
      v00.push_back(a);
  }
  int n11 = v11.size();
  int n01 = v01.size();
  int n10 = v10.size();
  if (n10 >= n01)
    total = partsum(v00, v10, v01);
  else
    total = partsum(v00, v01, v10);
  int m;
  m = min((int)v00.size(), n11);
  total = accumulate(v00.begin(), v00.begin() + m, total);
  total = accumulate(v11.begin(), v11.end(), total);
  cout << total << "\n";
  return 0;
}
