#include <bits/stdc++.h>
using namespace std;
vector<pair<string, int> > p;
bool vis[400005];
map<string, int> ma;
const int MAXN = 10000 + 10;
bool cmp(pair<string, int> a, pair<string, int> b) {
  if (a.first == b.first) return a.second > b.second;
  return a.first > b.first;
}
bool cmp1(pair<string, int> a, pair<string, int> b) {
  return a.second > b.second;
}
int main() {
  int n;
  cin >> n;
  string s;
  int ans;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    cin >> s >> ans;
    auto it = ma.find(s);
    if (it != ma.end())
      ma.at(s)++;
    else
      ma.emplace(s, 1);
    p.push_back(make_pair(s, ans));
  }
  sort(p.begin(), p.end(), cmp);
  string s1 = "11";
  long long sum = 0;
  for (int i = 0; i < p.size(); i++) {
    if (p.at(i).first != s1) break;
    sum += p.at(i).second;
    vis[i] = true;
  }
  bool t1 = false, t2 = false;
  int len, len1;
  auto it = ma.find("10");
  if (it != ma.end()) t1 = true, len = it->second;
  it = ma.find("01");
  if (it != ma.end()) t2 = true, len1 = it->second;
  if (t1 && t2) {
    len = min(len, len1);
    string s1 = "10";
    for (int i = 0; i < p.size(); i++) {
      if (p.at(i).first == s1) {
        for (int j = i; j < i + len; j++) sum += p.at(j).second, vis[j] = true;
        break;
      }
    }
    s1 = "01";
    for (int i = 0; i < p.size(); i++) {
      if (p.at(i).first == s1) {
        for (int j = i; j < i + len; j++) sum += p.at(j).second, vis[j] = true;
        break;
      }
    }
  }
  it = ma.find("11");
  if (it != ma.end()) {
    vector<pair<string, int> > p1;
    len = it->second;
    for (int i = 0; i < p.size(); i++) {
      if (!vis[i]) {
        p1.push_back(p.at(i));
      }
    }
    sort(p1.begin(), p1.end(), cmp1);
    for (int i = 0; i < p1.size() && i < len; i++) sum += p1.at(i).second;
  }
  cout << sum << endl;
}
