#include <bits/stdc++.h>
using namespace std;
int a_00[400010] = {0}, a_11[400010] = {0}, a_01[400010] = {0},
    a_10[400010] = {0}, b[400010] = {0};
bool cmp(int x, int y) { return x > y; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x == 0) {
      ++a_00[0];
      a_00[a_00[0]] = y;
    } else if (x == 10) {
      ++a_10[0];
      a_10[a_10[0]] = y;
    } else if (x == 1) {
      ++a_01[0];
      a_01[a_01[0]] = y;
    } else {
      ++a_11[0];
      a_11[a_11[0]] = y;
    }
  }
  sort(a_00 + 1, a_00 + a_00[0] + 1, cmp);
  sort(a_10 + 1, a_10 + a_10[0] + 1, cmp);
  sort(a_01 + 1, a_01 + a_01[0] + 1, cmp);
  long long s = 0;
  for (int i = 1; i <= a_11[0]; i++) s += a_11[i];
  int r = min(a_10[0], a_01[0]);
  for (int i = 1; i <= r; i++) s = s + a_10[i] + a_01[i];
  int x = a_11[0];
  int y = a_11[0];
  if (a_01[0] > a_10[0]) {
    for (int i = 1; i <= a_00[0]; i++) {
      b[i] = a_00[i];
      b[0]++;
    }
    for (int i = a_00[0] + 1, j = a_10[0] + 1; i <= a_00[0] + (a_01[0] - r);
         i++, j++) {
      b[i] = a_01[j];
      b[0]++;
    }
    sort(b + 1, b + b[0] + 1, cmp);
    for (int i = 1; i <= min(b[0], x); i++) {
      s += b[i];
    }
  } else if (a_01[0] < a_10[0]) {
    for (int i = 1; i <= a_00[0]; i++) {
      b[i] = a_00[i];
      b[0]++;
    }
    for (int i = a_00[0] + 1, j = a_01[0] + 1; i <= a_00[0] + (a_10[0] - r);
         i++, j++) {
      b[i] = a_10[j];
      b[0]++;
    }
    sort(b + 1, b + b[0] + 1, cmp);
    for (int i = 1; i <= min(b[0], x); i++) {
      s += b[i];
    }
  } else {
    for (int i = 1; i <= min(a_00[0], x); i++) s += a_00[i];
  }
  printf("%lld\n", s);
  return 0;
}
