#include <bits/stdc++.h>
using namespace std;
template <class T>
bool max_self(T& a, const T& b) {
  return b > a ? a = b, true : false;
}
template <class T>
bool min_self(T& a, const T& b) {
  return b < a ? a = b, true : false;
}
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);
int N, total = 0;
vector<int> both, fst, sec, none;
string util;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N;
  for (int rep = 0, x; rep < N; ++rep) {
    cin >> util >> x;
    if (util == "10")
      fst.push_back(x);
    else if (util == "01")
      sec.push_back(x);
    else if (util == "11")
      both.push_back(x);
    else
      none.push_back(x);
  }
  if (((int)(both).size()) + min(((int)(fst).size()), ((int)(sec).size())) ==
      0) {
    cout << "0\n";
    return 0;
  }
  for (int ele : both) total += ele;
  sort((fst).rbegin(), (fst).rend());
  sort((sec).rbegin(), (sec).rend());
  for (int idx = 0; idx < max(((int)(fst).size()), ((int)(sec).size()));
       ++idx) {
    if (idx >= ((int)(fst).size()))
      none.push_back(sec[idx]);
    else if (idx >= ((int)(sec).size()))
      none.push_back(fst[idx]);
    else
      total += sec[idx] + fst[idx];
  }
  sort((none).rbegin(), (none).rend());
  for (int rep = 0; rep < min(((int)(both).size()), ((int)(none).size()));
       ++rep)
    total += none[rep];
  cout << total << '\n';
  return 0;
}
