#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<long long int> v[4];
  for (int i = 0; i < t; i++) {
    string s;
    long long int k;
    cin >> s >> k;
    if (s == "00") v[0].push_back(k);
    if (s == "01") v[1].push_back(k);
    if (s == "10") v[2].push_back(k);
    if (s == "11") v[3].push_back(k);
  }
  sort(v[0].begin(), v[0].end(), less<long long int>());
  sort(v[1].begin(), v[1].end(), less<long long int>());
  sort(v[2].begin(), v[2].end(), less<long long int>());
  sort(v[3].begin(), v[3].end(), less<long long int>());
  reverse(v[3].begin(), v[3].end());
  reverse(v[2].begin(), v[2].end());
  reverse(v[1].begin(), v[1].end());
  reverse(v[0].begin(), v[0].end());
  long long int sum = 0;
  for (int j = 0; j < v[3].size(); j++) {
    sum += v[3][j];
  }
  int n = v[1].size(), m = v[2].size();
  for (int j = 0; j < min(n, m); j++) {
    sum += v[1][j] + v[2][j];
  }
  vector<long long int> v1;
  for (int j = min(n, m); j < n; j++) v1.push_back(v[1][j]);
  for (int j = min(n, m); j < m; j++) v1.push_back(v[2][j]);
  for (int j = 0; j < v[0].size(); j++) v1.push_back(v[0][j]);
  sort(v1.begin(), v1.end());
  for (int j = 0; j < v[3].size(); j++) {
    if (j < v1.size())
      sum += v1[v1.size() - 1 - j];
    else
      break;
  }
  cout << sum << endl;
}
