#include <bits/stdc++.h>
using namespace std;
priority_queue<long long> que[3];
int get(int x) {
  if (x == 0) return 0;
  if (x == 1) return 1;
  if (x == 10) return 2;
  if (x == 11) return 3;
}
long long ans = 0;
int sum = 0;
int main() {
  int n, p, c;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &p, &c);
    if (p == 11) {
      ans += c;
      sum++;
    } else
      que[get(p)].push(c);
  }
  while (!que[1].empty() && !que[2].empty()) {
    ans += que[1].top() + que[2].top();
    que[1].pop(), que[2].pop();
  }
  int x = que[1].empty() ? 2 : 1;
  while (!que[x].empty()) {
    que[0].push(que[x].top());
    que[x].pop();
  }
  while (sum-- && !que[0].empty()) {
    ans += que[0].top();
    que[0].pop();
  }
  cout << ans << endl;
  return 0;
}
