#include <bits/stdc++.h>   using namespace std;   const long o = 1e6 + 5;   long n, i, j, h, k, sl, g, kq = 0;   long a[o], b[o], c[o], A, B;   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     i = 0, j = 0, h = 0, k = 0;     for (long p = 1; p <= n; p++) {       cin >> s >> g;       if (s ==  00 ) {         i++;         a[i] = g;       } else if (s ==  01 ) {         j++;         b[j] = g;       } else if (s ==  10 ) {         h++;         c[h] = g;       } else if (s ==  11 ) {         k++;         kq += g;       }     }     if (k == 0 && (j == 0 || h == 0)) {       cout << 0;       return 0;     }     sort(a + 1, a + 1 + i);     sort(b + 1, b + 1 + j);     sort(c + 1, c + 1 + h);     a[0] = b[0] = c[0] = 0;     A = B = 0;     sl = min(h, j);     while (A < sl) {       A++;       kq += c[h];       h--;     }     while (B < sl) {       B++;       kq += b[j];       j--;     }     sl = 2 * (k + sl) - (k + 2 * sl);     for (long p = 1; p <= sl; p++) {       long ma = max(a[i], max(b[j], c[h]));       if (ma == 0) break;       kq += ma;       if (ma == a[i])         i--;       else if (ma == b[j])         j--;       else if (ma == c[h])         h--;     }     cout << kq;     return 0;   }   