#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    long long int n, al = 0, bob = 0, sum = 0, count = 0;
    cin >> n;
    long long int a[n], bob1[n], al1[n], none[n], p = 0, j = 0, k = 0, i;
    string s;
    for (i = 0; i < n; i++) {
      cin >> s >> a[i];
      if (s == "10") {
        al += 1;
        al1[j] = a[i];
        j++;
      } else if (s == "01") {
        bob += 1;
        bob1[k] = a[i];
        k++;
      } else if (s == "11") {
        sum = sum + a[i];
        count++;
      } else {
        none[p] = a[i];
        p++;
      }
    }
    long long int val;
    if (al < bob) {
      sort(bob1, bob1 + k);
      for (i = 0; i < j; i++) {
        sum = sum + al1[i];
      }
      for (i = k - al; i < k; i++) {
        sum = sum + bob1[i];
      }
      for (i = 0; i < k - al; i++) {
        none[p] = bob1[i];
        p++;
      }
      if (count > 0) {
        sort(none, none + p);
        val = count;
        if (p >= val) {
          for (i = p - val; i < p; i++) {
            sum = sum + none[i];
          }
        } else {
          for (i = 0; i < p; i++) {
            sum = sum + none[i];
          }
        }
      }
      cout << sum << "\n";
    } else if (al >= bob) {
      sort(al1, al1 + j);
      for (i = 0; i < k; i++) {
        sum = sum + bob1[i];
      }
      for (i = j - bob; i < j; i++) {
        sum = sum + al1[i];
      }
      for (i = 0; i < j - bob; i++) {
        none[p] = al1[i];
        p++;
      }
      if (count > 0) {
        sort(none, none + p);
        val = count;
        if (p >= val) {
          for (i = p - val; i < p; i++) {
            sum = sum + none[i];
          }
        } else {
          for (i = 0; i < p; i++) {
            sum = sum + none[i];
          }
        }
      }
      cout << sum << "\n";
    }
  }
}
