#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M) {
  os << "[";
  for (auto pr : M) os << pr.first << "->" << pr.second << ",";
  return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, unordered_map<L, R> M) {
  os << "[";
  for (auto pr : M) os << pr.first << "->" << pr.second << ",";
  return os << "]";
}
mt19937_64 mt(time(0));
struct Matrix {
  vector<vector<int>> data;
  int r, c;
  Matrix(int row, int col, bool identity = false) : r(row), c(col) {
    data.assign(row, vector<int>(col, 0));
    if (identity) {
      for (int i = 0; i < r; ++i) {
        data[i][i] = 1;
      }
    }
  }
  Matrix operator*(Matrix &other) {
    int m = r, n = c, p = other.c;
    Matrix res(m, p);
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < p; ++j) {
        for (int k = 0; k < n; ++k) {
          res.data[i][j] += data[i][k] * other.data[k][j];
        }
      }
    }
    return res;
  }
};
vector<int> extendGcd(int a, int b) {
  if (b == 0) {
    return {1, 0, a};
  } else {
    vector<int> tmp = extendGcd(b, a % b);
    return {tmp[1], tmp[0] - (a / b) * tmp[1], tmp[2]};
  }
}
Matrix matrix_power(Matrix base, long long exp) {
  int n = base.r;
  Matrix res(n, n, true);
  while (exp) {
    if (exp & 1) {
      res = res * base;
    }
    base = base * base;
    exp >>= 1;
  }
  return res;
}
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline double dbread() {
  double X = 0, Y = 1.0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar();
  ch = getchar();
  while (isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
int n;
vector<int> v[4];
vector<long long> pre[3];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> n;
  string s;
  int a;
  for (int i = 1; i <= n; ++i) {
    cin >> s >> a;
    v[((s[0] - '0') << 1) | (s[1] - '0')].push_back(a);
  }
  for (int i = 0; i <= 3; ++i)
    sort((v[i]).begin(), (v[i]).end(), greater<int>());
  for (int i = 0; i <= 2; ++i) {
    pre[i].push_back(0);
    for (int j = 0; j <= (int)v[i].size() - 1; ++j) {
      pre[i].push_back(pre[i].back() + v[i][j]);
    }
  }
  if (v[3].empty() && (v[1].empty() || v[2].empty())) {
    cout << 0;
    return 0;
  }
  long long res = 0;
  if (v[1].size() >= v[2].size()) {
    for (int i = (int)v[1].size(); i >= (int)v[2].size(); --i) {
      int diff = i - (int)v[2].size();
      int upper = min((int)v[0].size(), (int)v[3].size() - diff);
      if (upper < 0) continue;
      res = max(res, pre[0][upper] + pre[1][i] + pre[2].back());
    }
  } else {
    for (int i = (int)v[2].size(); i >= (int)v[1].size(); --i) {
      int diff = i - (int)v[1].size();
      int upper = min((int)v[0].size(), (int)v[3].size() - diff);
      if (upper < 0) continue;
      res = max(res, pre[0][upper] + pre[2][i] + pre[1].back());
    }
  }
  for (int i : v[3]) {
    res += i;
  }
  cout << res;
  return 0;
}
