#include <bits/stdc++.h>
using namespace std;
char s[4];
inline int read() {
  scanf("%s", s);
  return (*s ^ '0') << 1 | (s[1] ^ '0');
}
std::vector<int> v[3];
int main() {
  int n, m = 0, a, sum = 0;
  scanf("%d", &n);
  while (n--) {
    int t = read();
    scanf("%d", &a);
    if (t == 3)
      sum += a, m++;
    else
      v[t].push_back(a);
  }
  if (v[1].size() < v[2].size()) std::swap(v[1], v[2]);
  for (int i : v[2]) sum += i;
  auto it = v[1].begin() + v[2].size();
  std::nth_element(v[1].begin(), it, v[1].end(), greater<int>());
  for (auto i = v[1].begin(); i < it; i++) sum += *i;
  v[0].insert(v[0].end(), it, v[1].end());
  if (m < v[0].size()) {
    it = v[0].begin() + m;
    nth_element(v[0].begin(), it, v[0].end(), greater<int>());
    for (auto i = v[0].begin(); i < it; i++) sum += *i;
  } else
    for (int i : v[0]) sum += i;
  printf("%d\n", sum);
  return 0;
}
