#include <bits/stdc++.h>
const int MAX = 4e5 + 10;
using namespace std;
int q[MAX];
int w[MAX];
int e[MAX];
int r[MAX];
bool cmp(int a, int b) { return a > b; }
int main() {
  int n;
  int t, a;
  int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t >> a;
    if (t == 0) q[++ans1] = a;
    if (t == 1) w[++ans2] = a;
    if (t == 10) e[++ans3] = a;
    if (t == 11) r[++ans4] = a;
  }
  long long sum = 0;
  sort(w + 1, w + ans2 + 1, cmp);
  sort(e + 1, e + ans3 + 1, cmp);
  for (int i = 1; i <= ans4; i++) sum += r[i];
  for (int i = 1; i <= min(ans2, ans3); i++) sum += w[i] + e[i];
  if (ans2 > ans3)
    for (int i = ans3 + 1; i <= ans2; i++) q[++ans1] = w[i];
  else if (ans2 < ans3)
    for (int i = ans2 + 1; i <= ans3; i++) q[++ans1] = e[i];
  sort(q + 1, q + ans1 + 1, cmp);
  for (int i = 1; i <= ans4; i++) sum += q[i];
  printf("%lld\n", sum);
  return 0;
}
