#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, y;
  cin >> n;
  long long i;
  vector<long long> a[4];
  long long k = 0, ans = 0;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    if (x == 11) {
      k++;
      ans += y;
    } else if (x == 1) {
      a[0].push_back(y);
    } else if (x == 10) {
      a[1].push_back(y);
    } else {
      a[2].push_back(y);
    }
  }
  for (i = 0; i < 4; i++) {
    sort(a[i].begin(), a[i].end());
    reverse(a[i].begin(), a[i].end());
  }
  long long m = min(a[0].size(), a[1].size());
  for (i = 0; i < m; i++) {
    ans += (a[0][i] + a[1][i]);
  }
  for (i = m; i < a[0].size(); i++) a[3].push_back(a[0][i]);
  for (i = m; i < a[1].size(); i++) a[3].push_back(a[1][i]);
  for (i = 0; i < a[2].size(); i++) a[3].push_back(a[2][i]);
  sort(a[3].begin(), a[3].end());
  reverse(a[3].begin(), a[3].end());
  for (i = 0; i < a[3].size(); i++) {
    if (!k) break;
    ans += a[3][i];
    k--;
  }
  cout << ans << endl;
  return 0;
}
