#include <bits/stdc++.h>
using namespace std;
priority_queue<long long> q00, q01, q10;
string str;
long long solve(priority_queue<long long> q, int& remain) {
  long long ans = 0;
  while (q.empty() == false && q00.empty() == false && remain > 0) {
    if (q.top() >= q00.top()) {
      ans += q.top();
      q.pop();
      remain--;
    } else {
      ans += q00.top();
      q00.pop();
      remain--;
    }
  }
  while (q.empty() == false && remain > 0) {
    ans += q.top();
    q.pop();
    remain--;
  }
  while (q00.empty() == false && remain > 0) {
    ans += q00.top();
    q00.pop();
    remain--;
  }
  return ans;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    while (q00.empty() == false) q00.pop();
    while (q01.empty() == false) q01.pop();
    while (q10.empty() == false) q10.pop();
    long long ans = 0;
    int s = 0;
    for (int i = 1; i <= n; i++) {
      long long v;
      cin >> str >> v;
      if (str == "00") q00.push(v);
      if (str == "01") q01.push(v);
      if (str == "10") q10.push(v);
      if (str == "11") {
        ans += v;
        s++;
      }
    }
    int people = s;
    while (q01.empty() == false && q10.empty() == false) {
      ans += q01.top();
      ans += q10.top();
      q01.pop();
      q10.pop();
      s++;
      people += 2;
    }
    int remain = 2 * s - people;
    if (q01.empty() == false) {
      ans += solve(q01, remain);
    }
    if (q10.empty() == false) {
      ans += solve(q10, remain);
    }
    while (remain > 0 && q00.empty() == false) {
      ans += q00.top();
      remain--;
      q00.pop();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
