#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <typename T>
inline ostream& operator<<(ostream& os, const vector<T>& v) {
  bool first = true;
  os << "{";
  for (unsigned int i = 0; i < v.size(); i++) {
    if (!first) os << ",";
    os << v[i];
    first = false;
  }
  return os << "}";
}
template <typename T>
inline ostream& operator<<(ostream& os, const set<T>& v) {
  bool first = true;
  os << "{";
  for (typename set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
    if (!first) os << ",";
    os << *ii;
    first = false;
  }
  return os << "}";
}
template <typename T>
inline ostream& operator<<(ostream& os, const multiset<T>& v) {
  bool first = true;
  os << "{";
  for (typename multiset<T>::const_iterator ii = v.begin(); ii != v.end();
       ++ii) {
    if (!first) os << ",";
    os << *ii;
    first = false;
  }
  return os << "}";
}
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, const map<T1, T2>& v) {
  bool first = true;
  os << "{";
  for (typename map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
       ++ii) {
    if (!first) os << ",";
    os << *ii;
    first = false;
  }
  return os << "}";
}
template <typename T>
bool isPrime(T n) {
  T i, lmt = sqrt(n * 1.0);
  for (i = 2; i <= lmt; i++)
    if (n % i == 0) return false;
  return true;
}
template <typename T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <typename T>
T BigMod(T b, T p, T m) {
  if (p == 0)
    return 1 % m;
  else if (p & 1)
    return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
  else {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
}
template <typename T>
T power(T b, T p) {
  if (p == 0)
    return 1;
  else if (p & 1)
    return b * power(b, p - 1);
  else {
    T ret = power(b, p / 2);
    return ret * ret;
  }
}
template <typename T>
T ModInv(T b, T m) {
  if (isPrime(b))
    return BigMod(b, m - 2, m) % m;
  else if (gcd(b, m) == 1)
    return BigMod(b, m - 1, m) % m;
}
template <typename T>
T egcd(T a, T b, T& x, T& y) {
  T d;
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  } else {
    T x1, y1;
    d = egcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
  }
  return d;
}
long long toint(string s) {
  long long i, j, l;
  long long num;
  l = s.size();
  for (i = l - 1, j = 0LL, num = 0LL; i >= 0LL; i--, j++) {
    num += (s[i] - 48) * power(10LL, j);
  }
  return num;
}
const double pi = acos(-1);
const int inf = (1 << 30);
const long long INF = (1LL << 62) - 1;
const int mod = (int)1e9 + 7;
const long long MOD = (long long)1e9 + 7;
const double EPS = 1e-9;
const int dx8[] = {-1, 0, +1, -1, +1, -1, 0, +1};
const int dy8[] = {+1, +1, +1, 0, 0, -1, -1, -1};
const int dx4[] = {0, -1, +1, 0};
const int dy4[] = {+1, 0, 0, -1};
const int dx2[] = {+1, 0};
const int dy2[] = {0, +1};
const int dxkn[] = {1, -1, 1, -1, 2, -2, -2, 2};
const int dykn[] = {2, 2, -2, -2, 1, 1, -1, -1};
vector<int> none, alice, bob, baki;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, n, a, aa, bb, tot, cnt, sz, ans;
  string s;
  cin >> n;
  for (i = 0, aa = 0, bb = 0, tot = 0, ans = 0; i < n; i++) {
    cin >> s >> a;
    if (s == "00") none.push_back(a);
    if (s == "10") alice.push_back(a);
    if (s == "01") bob.push_back(a);
    if (s == "11") {
      ans += a;
      aa++;
      bb++;
      tot++;
    }
  }
  cnt = tot;
  sort(bob.begin(), bob.end(), greater<int>());
  sort(alice.begin(), alice.end(), greater<int>());
  sort(none.begin(), none.end(), greater<int>());
  sz = min(bob.size(), alice.size());
  for (i = 0; i < sz; i++) {
    ans += bob[i];
    ans += alice[i];
    aa++;
    bb++;
    tot += 2;
  }
  for (i = sz; i < alice.size(); i++) baki.push_back(alice[i]);
  for (i = sz; i < bob.size(); i++) baki.push_back(bob[i]);
  for (i = 0; i < none.size(); i++) baki.push_back(none[i]);
  sort(baki.begin(), baki.end(), greater<int>());
  sz = min(cnt, int(baki.size()));
  for (i = 0; i < sz; i++) {
    ans += baki[i];
  }
  cout << ans << "\n";
  return 0;
}
