#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5;
const long long INF = 1e18;
int main() {
  string type;
  long long n, i, cost;
  vector<long long> v1, v2, v3, v4;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> type >> cost;
    if (type == "00") v1.push_back(cost);
    if (type == "01") v2.push_back(cost);
    if (type == "10") v3.push_back(cost);
    if (type == "11") v4.push_back(cost);
  }
  sort(v2.begin(), v2.end());
  sort(v3.begin(), v3.end());
  sort(v4.begin(), v4.end());
  reverse(v2.begin(), v2.end());
  reverse(v3.begin(), v3.end());
  reverse(v4.begin(), v4.end());
  long long pay = 0;
  for (auto it : v4) pay += it;
  for (i = 0; i < min(v2.size(), v3.size()); ++i) pay += v2[i] + v3[i];
  if (v2.size() < v3.size())
    for (i = v2.size(); i < v3.size(); ++i) v1.push_back(v3[i]);
  else
    for (i = v3.size(); i < v2.size(); ++i) v1.push_back(v2[i]);
  sort(v1.begin(), v1.end());
  reverse(v1.begin(), v1.end());
  for (i = 0; i < min(v4.size(), v1.size()); ++i) pay += v1[i];
  cout << pay;
}
