#include <bits/stdc++.h>
using namespace std;
int N;
vector<pair<string, int>> spectators;
int sort_fn(pair<string, int> &lhs, pair<string, int> &rhs) {
  if (lhs.first > rhs.first) {
    return 1;
  }
  if (lhs.first < rhs.first) {
    return 0;
  }
  return lhs.second > rhs.second;
}
int main() {
  cin >> N;
  spectators.resize(N);
  for (int i = 0; i < N; i++) {
    string s;
    int infl;
    cin >> s >> infl;
    spectators[i].first = s;
    spectators[i].second = infl;
  }
  sort(spectators.begin(), spectators.end(), sort_fn);
  vector<int> boths, alices, bobs, nones;
  for (int i = 0; i < N; i++) {
    if (spectators[i].first == "11") {
      boths.push_back(spectators[i].second);
    } else if (spectators[i].first == "10") {
      alices.push_back(spectators[i].second);
    } else if (spectators[i].first == "01") {
      bobs.push_back(spectators[i].second);
    } else {
      nones.push_back(spectators[i].second);
    }
  }
  long long ans = 0;
  int x = boths.size();
  for (long long i : boths) {
    ans += i;
  }
  int a = min(alices.size(), bobs.size());
  int b = a;
  for (int i = 0; i < a; i++) {
    ans += alices[i];
    ans += bobs[i];
  }
  if (a == alices.size() && a == bobs.size()) {
    int n = min(x, (int)nones.size());
    for (int i = 0; i < n; i++) {
      ans += nones[i];
    }
    cout << ans << endl;
    return 0;
  }
  if (a == alices.size()) {
    int none_ptr = 0;
    int bob_ptr = a;
    if (nones.size() == 0 && x + a > b) {
      for (int i = bob_ptr; i < bobs.size(); i++) {
        ans += bobs[i];
        b++;
        if (x + a == b) break;
      }
      cout << ans << endl;
      return 0;
    }
    int n = 0;
    while (x + a > b + n && x + b > a + n) {
      if (bob_ptr >= bobs.size() && none_ptr >= nones.size()) break;
      if (bob_ptr >= bobs.size()) {
        n++;
        ans += nones[none_ptr];
        none_ptr++;
      } else if (none_ptr >= nones.size()) {
        b++;
        ans += bobs[bob_ptr];
        bob_ptr++;
      } else {
        if (bobs[bob_ptr] >= nones[none_ptr]) {
          b++;
          ans += bobs[bob_ptr];
          bob_ptr++;
        } else {
          n++;
          ans += nones[none_ptr];
          none_ptr++;
        }
      }
    }
  } else {
    int none_ptr = 0;
    int alice_ptr = b;
    if (nones.size() == 0 && x + b > a) {
      for (int i = alice_ptr; i < alices.size(); i++) {
        ans += alices[i];
        a++;
        if (x + b == a) break;
      }
      cout << ans << endl;
      return 0;
    }
    int n = 0;
    while (x + a > b + n && x + b > a + n) {
      if (alice_ptr >= alices.size() && none_ptr >= nones.size()) break;
      if (alice_ptr >= alices.size()) {
        n++;
        ans += nones[none_ptr];
        none_ptr++;
      } else if (none_ptr >= nones.size()) {
        a++;
        ans += alices[alice_ptr];
        alice_ptr++;
      } else {
        if (alices[alice_ptr] >= nones[none_ptr]) {
          a++;
          ans += alices[alice_ptr];
          alice_ptr++;
        } else {
          n++;
          ans += nones[none_ptr];
          none_ptr++;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
