#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int count = 0;
  priority_queue<int> a;
  priority_queue<int> b;
  priority_queue<int> c;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    if (s == "11") {
      ans += x;
      count++;
    } else if (s == "10") {
      a.push(x);
    } else if (s == "01") {
      b.push(x);
    } else {
      c.push(x);
    }
  }
  while (!a.empty() && !b.empty()) {
    ans = ans + a.top() + b.top();
    a.pop();
    b.pop();
  }
  while (!a.empty()) {
    c.push(a.top());
    a.pop();
  }
  while (!b.empty()) {
    c.push(b.top());
    b.pop();
  }
  while (count != 0 && !c.empty()) {
    ans = ans + c.top();
    c.pop();
    count--;
  }
  cout << ans;
}
