Elections

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 a i and b i (0 ≤ a i ≤ 105; 0 ≤ b i ≤ 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, a i is equal to zero, in which case b i 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

Solution:

#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;

const int MAXN = 100010;

int s[2][MAXN];

void modify(int q, int x, int v) {
while (x < MAXN) {
    s[q][x] += v;
    x = (x | (x - 1)) + 1;
  }
}

int find_sum(int q, int x) {
  int v = 0;
  while (x > 0) {
v += s[q][x];
x &= x - 1;
}
return v;
}

multiset <int> votes[MAXN];

int pred[MAXN], last[MAXN];
int danger[MAXN];

int main() {
int n;
scanf("%d", &n);
int my = 0;
for (int i = 0; i < MAXN; i++) {
    votes[i].clear();
  }
  for (int q = 0; q < 2; q++) {
    for (int i = 0; i < MAXN; i++) {
      s[q][i] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a == 0) {
      my++;
    } else {
      votes[a].insert(b);
      modify(0, b + 1, 1);
      modify(1, b + 1, b);
    }
  }
  for (int i = 0; i <= n; i++) {
    last[i] = 0;
  }
  for (int i = 1; i < MAXN; i++) {
    int cnt = votes[i].size();
    pred[i] = last[cnt];
    last[cnt] = i;
  }
  int ans = (int)2e9;
  int nd = 0;
  int add = 0;
  for (int win = n; win >= my; win--) {
int j = last[win];
while (j > 0) {
danger[nd++] = j;
j = pred[j];
}
for (int q = 0; q < nd; q++) {
      int j = danger[q];
      int u = *(votes[j].begin());
      add += u;
      my++;
      modify(0, u + 1, -1);
      modify(1, u + 1, -u);
      votes[j].erase(votes[j].begin());
    }
    int cur = add;
    if (my < win) {
      int need = win - my;
      int ll = 1, rr = MAXN - 1;
      while (ll < rr) {
        int mid = (ll + rr) >> 1;
int have = find_sum(0, mid);
if (have >= need) {
rr = mid;
} else {
ll = mid + 1;
}
}
int have = find_sum(0, ll);
int sum = find_sum(1, ll);
if (have > need) {
sum -= (have - need) * (ll - 1);
}
cur += sum;
}
if (cur < ans) {
      ans = cur;
    }
  }
  printf("%d\n", ans);
  return 0;
}