#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int Max = 4e5 + 10;
char a[3];
int b1[Max], b2[Max], b3[Max];
int cmp(int a, int b) { return a > b; }
int main() {
  int n, k = 0, k1 = 0, k2 = 0, k3 = 0;
  int ans = 0, b;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s%d", a, &b);
    if (strcmp(a, "11") == 0) {
      ans += b;
      k++;
    }
    if (strcmp(a, "10") == 0) b1[k1++] = b;
    if (strcmp(a, "01") == 0) b2[k2++] = b;
    if (strcmp(a, "00") == 0) b3[k3++] = b;
  }
  sort(b1, b1 + k1, cmp);
  sort(b2, b2 + k2, cmp);
  sort(b3, b3 + k3, cmp);
  int mins = min(k1, k2);
  for (int i = 0; i < mins; i++) {
    ans += b1[i];
    ans += b2[i];
  }
  int kk1 = mins, kk2 = mins, kk3 = 0;
  for (int i = 0; i < k; i++) {
    if (b1[kk1] >= b2[kk2] && b1[kk1] >= b3[kk3]) {
      ans += b1[kk1];
      kk1++;
    } else if (b2[kk2] >= b1[kk1] && b2[kk2] >= b3[kk3]) {
      ans += b2[kk2];
      kk2++;
    } else if (b3[kk3] >= b2[kk2] && b3[kk3] > b1[kk1]) {
      ans += b3[kk3];
      kk3++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
