#include <bits/stdc++.h>
inline long long rd() {
  long long _x = 0;
  int _ch = getchar(), _f = 1;
  for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar())
    ;
  if (_ch == '-') {
    _f = 0;
    _ch = getchar();
  }
  for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0';
  return _f ? _x : -_x;
}
void write(long long _x) {
  if (_x >= 10)
    write(_x / 10), putchar(_x % 10 + '0');
  else
    putchar(_x + '0');
}
inline void wrt(long long _x, char _p) {
  if (_x < 0) putchar('-'), _x = -_x;
  write(_x);
  if (_p) putchar(_p);
}
char s[400005][2];
int a[400005];
int n;
std::vector<int> H[4], tmp;
int main() {
  n = rd();
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i]);
    a[i] = rd();
    int x = ((s[i][0] - '0') << 1) + (s[i][1] - '0');
    H[x].push_back(a[i]);
  }
  std::sort(H[1].begin(), H[1].end(), std::greater<int>());
  std::sort(H[2].begin(), H[2].end(), std::greater<int>());
  long long ans = 0;
  for (auto i : H[3]) ans += i;
  int x = std::min(H[1].size(), H[2].size());
  for (int i = 0; i < x; i++) ans += H[1][i] + H[2][i];
  for (int i = x; i < H[1].size(); i++) tmp.push_back(H[1][i]);
  for (int i = x; i < H[2].size(); i++) tmp.push_back(H[2][i]);
  for (auto i : H[0]) tmp.push_back(i);
  sort(tmp.begin(), tmp.end(), std::greater<int>());
  for (int i = 0; i < std::min(tmp.size(), H[3].size()); i++) ans += tmp[i];
  wrt(ans, '\n');
}
