#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int num[maxn], ans = 1 << 30, N;
vector<int> G[maxn];
int q[maxn], cnt;
int check(int Mid) {
  int tnum = num[0] + Mid, res = 0, tot = 0;
  for (int i = 1; i <= 100000; i++) {
    for (int j = 0; j <= max(-1, num[i] - tnum); j++) tot++, res += G[i][j];
  }
  if (tot > Mid) return 1 << 30;
  cnt = 0;
  for (int i = 1; i <= 100000; i++) {
    for (int j = max(0, num[i] - tnum + 1); j <= num[i] - 1; j++)
      q[++cnt] = G[i][j];
  }
  sort(q + 1, q + cnt + 1);
  for (int i = 1; i <= Mid - tot; i++) res += q[i];
  return res;
}
int main() {
  int x, y;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &x, &y);
    if (x == 0 || y == 0)
      num[0]++;
    else
      G[x].push_back(y), num[x]++;
  }
  for (int i = 1; i <= 100000; i++) sort(G[i].begin(), G[i].end());
  int F = true;
  for (int i = 1; i <= N; i++)
    if (num[i] >= num[0]) F = false;
  if (F) return puts("0"), 0;
  int L = 1, R = N, Mid1, Mid2, f1, f2;
  while (L <= R) {
    Mid1 = L + (R - L) / 3;
    f1 = check(Mid1);
    Mid2 = R - (R - L) / 3;
    f2 = check(Mid2);
    if (f1 <= f2)
      R = Mid2 - 1, ans = min(ans, f1);
    else
      L = Mid1 + 1, ans = min(ans, f2);
  }
  printf("%d\n", ans);
  return 0;
}
