#include <bits/stdc++.h>
using namespace std;
vector<int> mas[7];
int pref[7][400005];
bool cmp(int a, int b) { return a > b; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string s;
    int val = 0;
    cin >> s >> val;
    if (s == "00") mas[0].push_back(val);
    if (s == "10") mas[1].push_back(val);
    if (s == "01") mas[2].push_back(val);
    if (s == "11") mas[3].push_back(val);
  }
  for (int i = 0; i < 4; i++) {
    sort(mas[i].begin(), mas[i].end(), cmp);
    pref[i][0] = 0;
    for (int j = 1; j <= (int)mas[i].size(); j++)
      pref[i][j] = pref[i][j - 1] + mas[i][j - 1];
  }
  int ans = 0;
  int d = (int)mas[3].size();
  for (int a = 0; a <= min((int)mas[0].size(), d); a++) {
    int t = d - a;
    int b = (int)mas[1].size(), c = (int)mas[2].size();
    if (b - t <= c) {
      int k = min(c, b + t);
      ans = max(ans, pref[0][a] + pref[1][b] + pref[2][k] + pref[3][d]);
    }
    if (c - t <= b) {
      int k = min(c + t, b);
      ans = max(ans, pref[0][a] + pref[1][k] + pref[2][c] + pref[3][d]);
    }
  }
  cout << ans;
  return 0;
}
