#include <bits/stdc++.h>
using namespace std;
const int maxn = 400050;
int n, x;
long long w;
vector<long long> val[4];
int Abs(int a) { return a < 0 ? -a : a; }
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%I64d", &x, &w);
    if (x == 11)
      val[0].push_back(w), sum += w;
    else if (x == 1)
      val[1].push_back(w), sum += w;
    else if (x == 10)
      val[2].push_back(w), sum += w;
    else
      val[3].push_back(w);
  }
  int len = val[3].size();
  int cnt = n - len;
  int num = min(val[1].size(), val[2].size());
  num += val[0].size();
  for (int i = 0; i < 4; i++) sort(val[i].begin(), val[i].end());
  long long ans = sum;
  int now1 = 0, now2 = 0;
  if (num * 2 < cnt) {
    ans = 0;
    int no = Abs(val[1].size() - val[2].size());
    if (val[2].size() < val[1].size()) {
      for (int i = 0; i < no; i++) {
        if (num * 2 >= cnt) break;
        sum -= val[1][i];
        cnt--;
        now1++;
        if (num * 2 >= cnt) ans = max(ans, sum);
      }
    } else {
      for (int i = 0; i < no; i++) {
        if (num * 2 >= cnt) break;
        sum -= val[2][i];
        cnt--;
        now2++;
        if (num * 2 >= cnt) ans = max(ans, sum);
      }
    }
    if (num * 2 >= cnt) ans = max(ans, sum);
  }
  if (num * 2 > cnt) {
    ans = sum;
    int pos = 1;
    while (num * 2 >= cnt) {
      if (pos > len) break;
      if (num * 2 >= (cnt + 1)) {
        sum += val[3][len - pos];
        ans = max(ans, sum);
        cnt++;
      } else
        break;
      pos++;
    }
    if (num * 2 >= cnt) ans = max(ans, sum);
    int no = Abs(val[2].size() - val[1].size());
    if (val[2].size() > val[1].size()) {
      for (int i = now2; i < no; i++) {
        if (pos + i - now2 > len) break;
        sum -= val[2][i];
        sum += val[3][len - i + now2 - pos];
        if (num * 2 >= cnt) ans = max(ans, sum);
      }
    } else {
      for (int i = now1; i < no; i++) {
        if (pos + i - now1 > len) break;
        sum -= val[1][i];
        sum += val[3][len - i + now1 - pos];
        if (num * 2 >= cnt) ans = max(ans, sum);
      }
    }
  } else if (num * 2 == cnt) {
    int no = Abs(val[2].size() - val[1].size());
    if (val[2].size() > val[1].size()) {
      for (int i = now2; i < no; i++) {
        if (i - now2 >= len) break;
        sum -= val[2][i];
        sum += val[3][len - i + now2 - 1];
        if (num * 2 >= cnt) ans = max(ans, sum);
      }
    } else {
      for (int i = now1; i < no; i++) {
        if (i - now1 >= len) break;
        sum -= val[1][i];
        sum += val[3][len - i + now1 - 1];
        if (num * 2 >= cnt) ans = max(ans, sum);
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
