#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  priority_queue<int> q1;
  priority_queue<int> q2;
  priority_queue<int> q3;
  priority_queue<int> q4;
  int s = 0;
  int tt = 0;
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a == 11) {
      s += b;
      tt++;
    } else if (a == 10)
      q2.push(b);
    else if (a == 1)
      q3.push(b);
    else if (a == 0)
      q4.push(b);
  }
  int t1 = q2.size();
  int t2 = q3.size();
  int t3 = min(t1, t2);
  for (int i = 1; i <= t3; i++) {
    s += (q2.top());
    s += (q3.top());
    q2.pop();
    q3.pop();
  }
  for (int i = 1; i <= tt; i++) {
    int a = 0, b = 0, c = 0;
    if (!q2.empty()) a = q2.top();
    if (!q3.empty()) b = q3.top();
    if (!q4.empty()) c = q4.top();
    if (a >= b && a >= c) {
      if (!q2.empty()) {
        s += a;
        q2.pop();
      }
    } else if (b >= a && b >= c) {
      if (!q3.empty()) {
        s += b;
        q3.pop();
      }
    } else if (c >= a && c >= b) {
      if (!q4.empty()) {
        s += c;
        q4.pop();
      }
    }
  }
  printf("%d\n", s);
  return 0;
}
