#include <bits/stdc++.h>
using namespace std;
long long spf[31];
long long fac[31];
void sieve() {
  spf[1] = 1;
  for (long long i = 2; i < 31; i++) spf[i] = i;
  for (long long i = 4; i < 31; i += 2) spf[i] = 2;
  for (long long i = 3; i * i < 31; i++) {
    if (spf[i] == i) {
      for (long long j = i * i; j < 31; j += i)
        if (spf[j] == j) spf[j] = i;
    }
  }
}
map<long long, long long> getfactor(long long a) {
  map<long long, long long> m;
  while (a > 1) {
    m[spf[a]]++;
    a /= spf[a];
  }
  return m;
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long inverse(long long a, long long p) { return power(a, p - 2, p); }
long long ncr(long long n, long long r, long long p) {
  if (r > n) return 0;
  if (r == 0) return 1;
  return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p;
}
long long n;
long long bit[1000005] = {0}, bit2[1000005] = {0};
long long get(long long index) {
  long long tot = 0;
  while (index > 0) {
    tot += bit[index];
    index -= index & (-index);
  }
  return tot;
}
void update(long long index, long long val) {
  while (index <= 5001) {
    bit[index] += val;
    index += index & (-index);
  }
}
long long get2(long long index) {
  long long tot = 0;
  while (index > 0) {
    tot += bit2[index];
    index -= index & (-index);
  }
  return tot;
}
void update2(long long index, long long val) {
  while (index <= 5001) {
    bit2[index] += val;
    index += index & (-index);
  }
}
long long bin(long long tot) {
  long long l = 0, r = 5000;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (get2(5000) - get2(mid) > tot) {
      l = mid + 1;
    } else
      r = mid - 1;
  }
  long long ans = get(5001) - get(l);
  long long count = get2(5001) - get2(l);
  if (count < tot) ans += (tot - count) * l;
  return ans;
}
void solve() {
  cin >> n;
  long long ans = 0;
  vector<long long> v1, v2;
  long long tot = 0, sum = 0, cnt = 0;
  for (long long i = 1; i <= n; i++) {
    string s;
    long long a;
    cin >> s >> a;
    if (s == "11") {
      sum += a;
      tot++;
      cnt++;
    } else if (s == "01") {
      update(a, a);
      update2(a, 1);
      v1.push_back(a);
    } else if (s == "10") {
      update(a, a);
      update2(a, 1);
      v2.push_back(a);
    } else {
      update(a, a);
      update2(a, 1);
    }
  }
  long long g = min(v1.size(), v2.size());
  sort(v1.begin(), v1.end(), greater<long long>());
  sort(v2.begin(), v2.end(), greater<long long>());
  ans = max(ans, sum + bin(tot));
  for (long long i = 0; i < g; i++) {
    sum += v1[i];
    update(v1[i], -v1[i]);
    update2(v1[i], -1);
    sum += v2[i];
    update(v2[i], -v2[i]);
    update2(v2[i], -1);
    tot++;
    cnt += 2;
    ans = max(ans, sum + bin(tot + tot - cnt));
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long test = 1;
  while (test--) {
    solve();
  }
}
