#include <bits/stdc++.h>   using namespace std;   long long a[1000005] = {0};   long long b[1000005] = {0};   long long c[1000005] = {0};   long long d[1000005] = {0};   long long s;   long long i, j = 0, l;   long long count1 = 0;   long long count2 = 0;   long long count3 = 0;   long long count4 = 0;   long long ans = 0;   bool cmp(long long aa, long long bb) { return aa > bb; }   int main() {     long long n;     scanf( %lld n , &n);     for (i = 0; i < n; i++) {       cin >> s >> j;       if (s == 11) {         a[count1++] = j;         ans += j;       } else if (s == 10) {         b[count2++] = j;       } else if (s == 1) {         c[count3++] = j;       } else {         d[count4++] = j;       }     }     sort(b, b + count2, cmp);     sort(c, c + count3, cmp);     for (i = 0; i < min(count2, count3); i++) ans += (b[i] + c[i]);     for (i = 0; i < abs(count2 - count3); i++) {       if (count3 > count2)         d[count4 + i] = c[min(count2, count3) + i];       else if (count3 < count2)         d[count4 + i] = b[min(count2, count3) + i];     }     count4 += abs(count2 - count3);     sort(d, d + count4, cmp);     for (i = 0; i < count1 && i < count4; i++) ans += d[i];     cout << ans << endl;     return 0;   }   