#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, unsigned long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
long long int powermod(long long int x, unsigned long long int y,
                       long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int32_t main() {
  long long int n;
  cin >> n;
  vector<long long int> oo, oz, zo, zz;
  for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
    string s;
    long long int p;
    cin >> s >> p;
    if (s == "11")
      oo.push_back(p);
    else if (s == "01")
      zo.push_back(p);
    else if (s == "10")
      oz.push_back(p);
    else
      zz.push_back(p);
  }
  sort((oo).begin(), (oo).end());
  reverse((oo).begin(), (oo).end());
  sort((oz).begin(), (oz).end());
  reverse((oz).begin(), (oz).end());
  sort((zo).begin(), (zo).end());
  reverse((zo).begin(), (zo).end());
  sort((zz).begin(), (zz).end());
  reverse((zz).begin(), (zz).end());
  long long int mn1 = min(oo.size(), zz.size());
  long long int ans = 0;
  long long int j = 0, k = 0, l = 0;
  long long int mn2 = min(oz.size(), zo.size());
  for (long long int i = (long long int)(0); i < (long long int)(mn2); i++) {
    ans += oz[k];
    ans += zo[l];
    k++;
    l++;
  }
  vector<long long int> final;
  for (long long int i = (long long int)(k); i < (long long int)(oz.size());
       i++) {
    final.push_back(oz[i]);
  }
  for (long long int i = (long long int)(l); i < (long long int)(zo.size());
       i++) {
    final.push_back(zo[i]);
  }
  for (long long int i = (long long int)(0); i < (long long int)(zz.size());
       i++) {
    final.push_back(zz[i]);
  }
  sort((final).begin(), (final).end());
  reverse((final).begin(), (final).end());
  j = 0;
  for (long long int i = (long long int)(0); i < (long long int)(oo.size());
       i++) {
    ans += oo[i];
    if (j < final.size()) ans += final[j];
    j++;
  }
  cout << ans << "\n";
  return 0;
}
