#include <bits/stdc++.h>
using namespace std;
int n, i, b, p;
string a;
vector<int> v[4];
int main() {
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> a >> b;
    if (a == "01")
      v[0].push_back(b);
    else if (a == "10")
      v[1].push_back(b);
    else if (a == "00")
      v[2].push_back(b);
    else if (a == "11")
      v[3].push_back(b);
  }
  for (i = 0; i < 4; ++i) {
    sort(v[i].begin(), v[i].end());
    reverse(v[i].begin(), v[i].end());
  }
  for (i = 0; i < min(v[0].size(), v[1].size()); ++i) {
    p += v[0][i] + v[1][i];
  }
  for (i = 0; i < v[3].size(); ++i) {
    p += v[3][i];
  }
  for (i = min(v[0].size(), v[1].size()); i < max(v[0].size(), v[1].size());
       ++i) {
    if (v[0].size() > v[1].size()) {
      v[2].push_back(v[0][i]);
    } else {
      v[2].push_back(v[1][i]);
    }
  }
  sort(v[2].begin(), v[2].end());
  reverse(v[2].begin(), v[2].end());
  for (i = 0; i < min(v[3].size(), v[2].size()); ++i) {
    p += v[2][i];
  }
  cout << p << "\n";
}
