#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T>
T read() {
  T p = 1, x = 0;
  char s = getchar();
  while (s == ' ' || s == '\n') s = getchar();
  if (s == '-') p = -1, s = getchar();
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * p;
}
template <typename A, typename B>
void Umax(A &a, const B &b) {
  if (a < b) a = b;
}
template <typename A, typename B>
void Umin(A &a, const B &b) {
  if (a > b) {
    a = b;
  }
}
long long bin_pow(long long a, long long n) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 1) {
    long long cnt = a * bin_pow(a, n - 1);
    return cnt;
  } else {
    long long cnt = bin_pow(a, n / 2);
    return cnt * cnt;
  }
}
const int N = (int)1e6 + 10;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a[4];
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int c;
    cin >> c;
    if (s == "00") a[0].push_back(c);
    if (s == "01") a[1].push_back(c);
    if (s == "10") a[2].push_back(c);
    if (s == "11") a[3].push_back(c);
  }
  long long l = 0, r = 0, s = 0, ans = 0;
  for (auto i : a[3]) {
    s++;
    l++;
    r++;
    ans += i;
  }
  for (int i = 0; i < 3; i++) {
    sort(a[i].begin(), a[i].end());
    reverse(a[i].begin(), a[i].end());
  }
  int mxx = min(a[1].size(), a[2].size());
  for (int i = 0; i < mxx; i++) {
    l++;
    r++;
    s += 2;
    ans = ans + a[1][i] + a[2][i];
  }
  long long ch = 0, prev = 0;
  vector<pair<int, int> > ts;
  for (int i = 0; i < 3; i++) {
    if (i != 0) {
      for (int j = mxx; j < a[i].size(); j++)
        ts.push_back(make_pair(a[i][j], i));
    } else
      for (int j = 0; j < a[i].size(); j++) ts.push_back(make_pair(a[i][j], i));
  }
  sort(ts.begin(), ts.end());
  reverse(ts.begin(), ts.end());
  for (auto i : ts) {
    if ((i.second == 0) && (2 * l >= (s + 1) && 2 * r >= (s + 1))) {
      ans += i.first;
      s++;
    } else if ((i.second == 1) &&
               (2 * l >= (s + 1) && 2 * (r + 1) >= (s + 1))) {
      ans += i.first;
      s++;
      r++;
    } else if ((i.second == 2) &&
               (2 * (l + 1) >= (s + 1) && 2 * r >= (s + 1))) {
      ans += i.first;
      s++;
      l++;
    }
  }
  cout << ans;
  return 0;
}
