#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int N = 2e6 + 10;
const long long mod = 1e9 + 7;
const int base = 131;
const double pi = acos(-1);
const double eps = 1e-8;
long long vis[N], num[N], n, m, k, x, y, z;
long long a[N], b[N];
long long cx, cy, cnt, ans, sum, flag, t, ff, res;
vector<int> v[N];
map<long long, long long> mp;
set<int> sa, sb;
pair<int, int> p[N];
string s;
long long check(long long mid) {
  long long x = mid + cnt;
  sum = 0;
  res = 0;
  for (int i = 1; i <= 100000; i++) {
    for (int j = 0; j <= v[i].size() - x; j++) sum++, res += v[i][j];
  }
  if (sum > mid) return 1e10;
  t = 0;
  for (int i = 1; i <= 100000; i++)
    for (int j = max(0, (int)(v[i].size() - x + 1)); j < v[i].size(); j++)
      num[++t] = v[i][j];
  sort(num + 1, num + t + 1);
  for (int i = 1; i <= mid - sum; i++) res += num[i];
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if (a[i] == 0 || b[i] == 0)
      cnt++;
    else {
      v[a[i]].push_back(b[i]);
    }
  }
  for (int i = 1; i <= 100000; i++) {
    sort(v[i].begin(), v[i].end());
    if (v[i].size() >= cnt) flag = 1;
  }
  if (!flag) return cout << 0 << endl, 0;
  long long l = 1, r = n, m1, m2;
  ans = 1e10;
  while (l <= r) {
    m1 = l + (r - l) / 3;
    m2 = r - (r - l) / 3;
    long long f1 = check(m1), f2 = check(m2);
    if (f1 <= f2) {
      r = m2 - 1;
      ans = min(ans, f1);
    } else
      l = m1 + 1, ans = min(ans, f2);
  }
  cout << ans << endl;
}
