#include <bits/stdc++.h>
using namespace std;
long long int N, n0;
multiset<long long int> bs;
map<long long int, vector<long long int> > per_a;
long long int f(long long int sum_n) {
  multiset<long long int> tmp_bs(bs);
  long long int res = 0;
  long long int removed = 0;
  for (auto it = per_a.begin(); it != per_a.end(); ++it) {
    long long int diff = it->second.size() - n0 - sum_n + 1;
    if (diff < 0) continue;
    if (diff > (long long int)it->second.size()) return -1;
    removed += diff;
    for (int i = 0; i < diff; ++i) {
      res += it->second[i];
      tmp_bs.erase(tmp_bs.find(it->second[i]));
    }
  }
  long long int to_remove = sum_n - removed;
  if (to_remove < 0) return -1;
  for (auto it = tmp_bs.begin(); 0 < to_remove && it != tmp_bs.end();
       ++it, --to_remove) {
    res += *it;
  }
  return res;
}
int main(int argc, char* argv[]) {
  std::ios::sync_with_stdio(false);
  cin >> N;
  n0 = 0;
  long long int lo = 0, up = 0;
  for (long long int i = 0; i < N; ++i) {
    long long int a, b;
    cin >> a >> b;
    if (0 == a) {
      ++n0;
      continue;
    }
    ++up;
    bs.insert(b);
    auto it = per_a.find(a);
    if (it != per_a.end()) {
      it->second.push_back(b);
    } else
      per_a[a] = vector<long long int>(1, b);
  }
  for (auto it = per_a.begin(); it != per_a.end(); ++it)
    sort(it->second.begin(), it->second.end());
  while (2 < up - lo) {
    long long int left = (2 * lo + up) / 3;
    long long int right = (lo + 2 * up) / 3;
    long long int f_left = f(left), f_right = f(right);
    if (f_left < 0)
      lo = left;
    else if (f_right < 0)
      lo = right;
    else if (f_left < f_right)
      up = right;
    else
      lo = left;
  }
  long long int current_min = LLONG_MAX;
  for (long long int i = lo; i <= up; ++i) {
    long long int f_i = f(i);
    if (0 <= f_i) current_min = min(current_min, f_i);
  }
  cout << current_min << endl;
  return 0;
}
