#include <bits/stdc++.h>
using namespace std;
long long sol;
int N, num, cant;
char C[2];
vector<int> cerouno, unocero, all;
int main() {
  scanf("%d\n", &N);
  for (int i = 1, a; i <= N; i++) {
    scanf("%s %d", &C, &a);
    if (C[0] == '1' && C[1] == '1') sol += a, num++, cant++;
    if (C[0] == '0' && C[1] == '1') cerouno.push_back(a);
    if (C[0] == '1' && C[1] == '0') unocero.push_back(a);
    if (C[0] == '0' && C[1] == '0') all.push_back(a);
  }
  sort(cerouno.begin(), cerouno.end());
  reverse(cerouno.begin(), cerouno.end());
  sort(unocero.begin(), unocero.end());
  reverse(unocero.begin(), unocero.end());
  int _min = min((int)unocero.size(), (int)cerouno.size());
  for (int i = 0; i < _min; ++i) {
    sol += unocero[i];
    sol += cerouno[i];
    num += 2, cant++;
  }
  num = 2 * cant - num;
  for (int i = _min; i < (int)unocero.size(); ++i) all.push_back(unocero[i]);
  for (int i = _min; i < (int)cerouno.size(); ++i) all.push_back(cerouno[i]);
  sort(all.begin(), all.end());
  reverse(all.begin(), all.end());
  for (int i = 0; i < (int)all.size() && i < num; ++i) {
    sol += all[i];
  }
  printf("%lld\n", sol);
}
