#include <bits/stdc++.h>
using namespace std;
bool cmp(int x, int y) { return x > y; }
int max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
int min(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int main() {
  vector<int> num1;
  vector<int> num2;
  vector<int> num3;
  vector<int> num4;
  vector<int> ans;
  int n;
  cin >> n;
  int tmp, value;
  for (int i = 0; i < n; i++) {
    cin >> tmp >> value;
    switch (tmp) {
      case 11: {
        num1.push_back(value);
        break;
      }
      case 10: {
        num2.push_back(value);
        break;
      }
      case 1: {
        num3.push_back(value);
        break;
      }
      case 0: {
        num4.push_back(value);
        break;
      }
      default:
        break;
    }
  }
  sort(num2.begin(), num2.end());
  sort(num3.begin(), num3.end());
  sort(num4.begin(), num4.end());
  for (auto iter : num1) ans.push_back(iter);
  int l = num2.size() - 1;
  int r = num3.size() - 1;
  int z = num4.size() - 1;
  while (l >= 0 && r >= 0) {
    ans.push_back(num2[l]);
    ans.push_back(num3[r]);
    l--;
    r--;
  }
  int max_num = 2 * (num1.size() + min(num2.size(), num3.size()));
  int left_num = max_num - ans.size();
  if (l < 0 && r >= 0) {
    for (int i = 0; i <= r; i++) num4.push_back(num3[i]);
  } else if (r < 0 && r <= 0) {
    for (int i = 0; i <= l; i++) num4.push_back(num2[i]);
  }
  sort(num4.begin(), num4.end(), cmp);
  for (int i = 0; i < left_num && i < num4.size(); i++) {
    ans.push_back(num4[i]);
  }
  int sum = 0;
  for (auto iter : ans) sum += iter;
  cout << sum << endl;
  return 0;
}
