#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
struct node {
  int a, b = 0;
} s[444444], s1[888888], s2[888888], s3[888888];
int cmp(node a, node b) { return a.b > b.b; }
int main() {
  int n, sum = 0, p1 = 0, p2 = 0, p3 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &s[i].a, &s[i].b);
    if (s[i].a == 1)
      s1[p1++] = s[i];
    else if (s[i].a == 0)
      s2[p2++] = s[i];
    else if (s[i].a == 10)
      s3[p3++] = s[i];
    else
      sum += s[i].b;
  }
  sort(s1, s1 + p1, cmp);
  sort(s3, s3 + p3, cmp);
  int p = min(p1, p3), s11 = n - p1 - p2 - p3;
  for (int i = 0; i < p; i++) sum += s3[i].b + s1[i].b;
  if (p1 > p3) {
    for (int i = p3; i < p1; i++) s2[p2++] = s1[i];
  } else {
    for (int i = p1; i < p3; i++) s2[p2++] = s3[i];
  }
  sort(s2, s2 + p2, cmp);
  int left = 2 * (p + s11) - (2 * p + s11);
  for (int i = 0; i < left; i++) sum += s2[i].b;
  printf("%d\n", sum);
}
