#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
vector<long long> v[4];
int p[5];
long long pre_max[maxn], pre_00[maxn];
bool cmp(long long a, long long b) { return a > b; }
int main() {
  char op[5];
  int n, cnt = 0;
  long long ans = 0, val;
  p[1] = p[2] = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s %lld", op, &val);
    if (strcmp(op, "00") == 0) {
      v[0].push_back(val);
    } else if (strcmp(op, "10") == 0) {
      v[1].push_back(val);
    } else if (strcmp(op, "01") == 0) {
      v[2].push_back(val);
    } else {
      v[3].push_back(val);
      ans += val;
      ++p[1];
      ++p[2];
      ++cnt;
    }
  }
  for (int i = 0; i <= 3; i++) {
    sort(v[i].begin(), v[i].end(), cmp);
  }
  int min_id = 1, max_id = 2;
  if (v[1].size() > v[2].size()) {
    min_id = 2;
    max_id = 1;
  }
  for (int i = 0; i < v[min_id].size(); i++) {
    ans += v[1][i] + v[2][i];
    ++p[1];
    ++p[2];
    cnt += 2;
  }
  int limit = v[max_id].size() - v[min_id].size();
  pre_max[0] = pre_00[0] = 0;
  for (int i = 1; i <= limit; i++) {
    pre_max[i] = pre_max[i - 1] + v[max_id][v[min_id].size() + i - 1];
  }
  for (int i = 0; i < v[0].size(); i++) {
    pre_00[i + 1] = pre_00[i] + v[0][i];
  }
  long long temp = ans;
  for (int i = 0; i <= limit; i++) {
    if (2 * p[min_id] - cnt - i >= 0) {
      int real = min(2 * p[min_id] - cnt - i, (int)v[0].size());
      long long res = temp + pre_max[i] + pre_00[real];
      ans = max(ans, res);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
