#include <bits/stdc++.h>   using namespace std;   long long n, s, a, ans, a11, r;   vector<long long> aa, ab, c;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> s >> a;       if (s == 11) {         a11++;         ans += a;       }       if (s == 0) c.push_back(a);       if (s == 10) aa.push_back(a);       if (s == 1) ab.push_back(a);     }     sort(aa.begin(), aa.end());     reverse(aa.begin(), aa.end());     sort(ab.begin(), ab.end());     reverse(ab.begin(), ab.end());     r = min(aa.size(), ab.size());     for (int i = r; i < aa.size(); i++) {       c.push_back(aa[i]);     }     for (int i = r; i < ab.size(); i++) {       c.push_back(ab[i]);     }     sort(c.begin(), c.end());     reverse(c.begin(), c.end());     for (int i = 0; i < min(a11, (long long)c.size()); i++) ans += c[i];     for (int i = 0; i < r; i++) {       ans += aa[i];       ans += ab[i];     }     cout << ans;   }   