#include <bits/stdc++.h>
using namespace std;
inline void read(long long &x) {
  x = 0;
  long long f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    s = getchar();
  }
  x *= f;
}
inline long long Max(long long x, long long y) { return x > y ? x : y; }
inline long long Min(long long x, long long y) { return x < y ? x : y; }
long long Gcd(long long a, long long b) {
  if (!b) return a;
  return Gcd(b, a % b);
}
long long Num[100005], Maxx, Want[100005];
vector<long long> T[100005];
inline long long Buy(long long Yao) {
  long long End = Num[0] + Yao, Sum = 0, Cha = 0;
  for (register int i = 1; i <= Maxx; ++i)
    for (register int j = 0; j <= Max(-1, Num[i] - End); ++j)
      Cha++, Sum += T[i][j];
  if (Cha > Yao) return 0x3f3f3f3f;
  long long Mai = 0;
  for (register int i = 1; i <= Maxx; ++i)
    for (register int j = Max(0, Num[i] - End + 1); j < Num[i]; ++j)
      Want[++Mai] = T[i][j];
  sort(Want + 1, Want + 1 + Mai);
  for (register int i = 1; i <= Yao - Cha; ++i) Sum += Want[i];
  return Sum;
}
int main() {
  long long n;
  read(n);
  for (register int i = 1; i <= n; ++i) {
    long long a, b;
    read(a);
    read(b);
    Maxx = Max(Maxx, a);
    if (!a || !b)
      Num[0]++;
    else
      T[a].push_back(b), Num[a]++;
  }
  for (register int i = 1; i <= Maxx; ++i) sort(T[i].begin(), T[i].end());
  bool Win = 1;
  for (register int i = 1; i <= Maxx; ++i)
    if (Num[i] >= Num[0]) Win = 0;
  if (Win) return !printf("0");
  long long l = 1, r = n, Ans = 0x3f3f3f3f;
  while (l <= r) {
    long long Mid1 = l + (r - l) / 3;
    long long Mid2 = r - (r - l) / 3;
    long long Use1 = Buy(Mid1);
    long long Use2 = Buy(Mid2);
    if (Use1 <= Use2)
      r = Mid2 - 1, Ans = Min(Ans, Use1);
    else
      l = Mid1 + 1, Ans = Min(Ans, Use2);
  }
  printf("%lld", Ans);
  return 0;
}
