#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 9;
vector<long long int> v[4];
long long int solve() {
  int n;
  cin >> n;
  string s;
  long long int l, a(0), b(0), x(0), sum(0);
  for (int i = 1; i <= n; ++i) {
    cin >> s >> l;
    if (s == "00")
      v[0].push_back(l);
    else if (s == "01")
      v[1].push_back(l);
    else if (s == "10")
      v[2].push_back(l);
    else if (s == "11")
      v[3].push_back(l);
  }
  for (int i = 0; i < 4; ++i) sort(v[i].begin(), v[i].end());
  if (v[1].size() <= v[2].size())
    x = v[1].size();
  else
    x = v[2].size();
  while (x--) {
    sum += v[1].back();
    sum += v[2].back();
    v[1].pop_back();
    v[2].pop_back();
  }
  for (auto i : v[3]) sum += i;
  if (x == -1) {
    x = v[3].size();
    vector<long long int> temp;
    for (int i = 0; i < 3; ++i) {
      for (auto it : v[i]) temp.push_back(it);
    }
    sort(temp.rbegin(), temp.rend());
    for (int i = 0; i < x and i < temp.size(); ++i) sum += temp[i];
    cout << sum;
    return 0LL;
  }
  x = v[3].size();
  while (x--) {
    if (v[0].empty()) break;
    sum += v[0].back();
    v[0].pop_back();
  }
  cout << sum << '\n';
  return 0LL;
}
signed main() {
  long long int t;
  t = 1;
  while (t--) {
    solve();
  }
}
