You are running for a governor in a small city in Russia. You ran some polls and did some research, and for every person in the city you know whom he will vote for, and how much it will cost to bribe that person to vote for you instead of whomever he wants to vote for right now. You are curious, what is the smallest amount of money you need to spend on bribing to win the elections. To win elections you need to have strictly more votes than any other candidate.

Input

First line contains one integer n (1 ≤ n ≤ 105) — number of voters in the city. Each of the next n lines describes one voter and contains two integers ai and bi (0 ≤ ai ≤ 105; 0 ≤ bi ≤ 104) — number of the candidate that voter is going to vote for and amount of money you need to pay him to change his mind. You are the candidate 0 (so if a voter wants to vote for you, ai is equal to zero, in which case bi will also be equal to zero).

Output

Print one integer — smallest amount of money you need to spend to win the elections.

Examples

Input

5
1 2
1 2
1 2
2 1
0 0


Output

3


Input

4
1 2
1 2
2 1
0 0


Output

2


Input

1
100000 0


Output

0
#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;
}
