#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 2e5 + 7;
const int maxm = 2e5 + 7;
const int mod = 998244353;
int n;
vector<int> a[4];
int type, x;
int ans;
int cnt, cnt_a, cnt_b;
bool check() { return (cnt_a * 2 >= cnt && cnt_b * 2 >= cnt); }
int main(int argc, char const *argv[]) {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &type, &x);
    if (type == 10) type = 2;
    if (type == 11) type = 3;
    a[type].push_back(x);
  }
  for (int i = 0; i < 4; i++) {
    sort(a[i].begin(), a[i].end());
    reverse(a[i].begin(), a[i].end());
  }
  for (int i = 0; i < a[3].size(); i++) {
    ans += a[3][i];
    cnt++;
    cnt_b++;
    cnt_a++;
  }
  int min_len = min(a[1].size(), a[2].size());
  for (int i = 0; i < min_len; i++) {
    ans += a[1][i];
    ans += a[2][i];
    cnt_b++;
    cnt_a++;
    cnt += 2;
  }
  if (a[1].size() > min_len) {
    for (int i = min_len; i < a[1].size(); i++) a[0].push_back(a[1][i]);
  }
  if (a[2].size() > min_len) {
    for (int i = min_len; i < a[2].size(); i++) a[0].push_back(a[2][i]);
  }
  sort(a[0].begin(), a[0].end());
  reverse(a[0].begin(), a[0].end());
  for (int i = 0; i < a[0].size(); i++) {
    cnt++;
    if (!check()) break;
    ans += a[0][i];
  }
  printf("%d\n", ans);
  return 0;
}
