#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int lowbit(int x) { return x & -x; }
int a[300007] = {0}, b[300007] = {0}, c[300007] = {0}, d[300007] = {0};
int num[300007] = {0}, q[300007] = {0};
int i = 0, j = 0, n = 0, m = 0, v1 = 0, v2 = 0;
long long k = 0;
int x = 0, y = 0, z = 0;
int t;
int ans = mod;
int cnt = 0;
string s, s1;
vector<int> ve[300007];
int ex, ey;
int cal(int numm) {
  int my = num[0] + numm, add = 0, res = 0, cnt = 0;
  for (int i = 1; i < 100100; i++) {
    for (j = 0; j <= max(-1, num[i] - my); j++) {
      add++;
      res += ve[i][j];
      if (add > numm) {
        return mod;
      }
    }
  }
  for (int i = 1; i < 100100; i++) {
    for (j = max(0, num[i] - my + 1); j < num[i]; j++) {
      q[++cnt] = ve[i][j];
    }
  }
  sort(q + 1, q + cnt + 1);
  for (int i = 1; i <= numm - add; i++) res += q[i];
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    if (x) ve[x].push_back(y);
    num[x]++;
  }
  bool be = 1;
  for (int i = 1; i < 100100; i++)
    if (num[i] > num[0]) {
      be = 0;
      break;
    }
  if (be) {
    cout << 0;
    return 0;
    ;
  }
  for (int i = 1; i < 100100; i++) {
    sort(ve[i].begin(), ve[i].end());
  }
  int l = 1, r = n, mid1, mid2, ans1, ans2;
  while (l <= r) {
    mid1 = l + (r - l) / 3;
    mid2 = r - (r - l) / 3;
    ans1 = cal(mid1);
    ans2 = cal(mid2);
    if (ans1 <= ans2) {
      r = mid2 - 1;
      ans = min(ans, ans1);
    } else {
      l = mid1 + 1;
      ans = min(ans, ans2);
    }
  }
  cout << ans << endl;
  return 0;
}
