#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long INF = 1e18L + 7;
template <class n, class second>
ostream &operator<<(ostream &p, pair<n, second> x) {
  return p << "<" << x.first << ", " << x.second << ">";
}
template <class n>
auto operator<<(ostream &p, n y) ->
    typename enable_if<!is_same<n, string>::value,
                       decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++) p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << '\n'; }
template <class n, class... second>
void dor(n p, second... y) {
  cerr << p << " ";
  dor(y...);
}
template <class n, class second>
void mini(n &p, second y) {
  if (p > y) p = y;
}
template <class n, class second>
void maxi(n &p, second y) {
  if (p < y) p = y;
}
map<int, vector<int> > M;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    ;
    M[a].push_back(b);
  }
  for (auto &i : M) {
    sort((i.second).begin(), (i.second).end());
  }
  int all = 0, jed = 0, dwa = 0, sum = 0;
  for (auto i : M[11]) {
    all += i;
    jed++;
    sum++;
    dwa++;
  };
  while (M[10].size() && M[1].size()) {
    all += M[10].back();
    all += M[1].back();
    M[10].pop_back();
    M[1].pop_back();
    sum += 2;
    jed++;
    dwa++;
  }
  if (M[1].size() < M[10].size()) swap(M[1], M[10]);
  ;
  while (M[1].size() || M[0].size()) {
    if (dwa * 2 >= sum + 1) {
      if (M[0].size() == 0 || (M[1].size() && M[1].back() > M[0].back())) {
        all += M[1].back();
        M[1].pop_back();
      } else {
        all += M[0].back();
        M[0].pop_back();
      }
      jed++;
      sum++;
    } else
      break;
  };
  cout << all << '\n';
}
