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;
const int MAXN = 100001;
const long long MOD = 998244353;
const int MAXL = 8000100;
const long long INF = 1e18;
int N, a, b, c;
vector<long long> V[MAXN];
int R;
long long ans;
priority_queue<pair<long long, long long> > pq;
struct node {
  int second, e, m, v, c;
  node *l, *r;
  node(int _s, int _e) : second(_s), e(_e), v(0), c(0) {
    m = (second + e) / 2;
    if (second != e) {
      l = new node(second, m);
      r = new node(m + 1, e);
    }
  }
  void ins(int x, int t) {
    assert(x <= e && x >= second);
    if (second == e) {
      c += t;
      v += t * x;
      return;
    }
    if (x <= m)
      l->ins(x, t);
    else
      r->ins(x, t);
    v = l->v + r->v;
    c = l->c + r->c;
  }
  int ask(int w) {
    if (w == c) return v;
    if (second == e) {
      return w * second;
    }
    if (l->c >= w)
      return l->ask(w);
    else
      return r->ask(w - l->c) + l->v;
  }
  void db() {
    if (second == e) {
      if (second <= 10) cout << v << ' ' << c << '\n';
    } else {
      l->db();
      r->db();
    }
  }
} * root;
int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    if (a == 0 || b == 0)
      ++R;
    else
      V[a].emplace_back(b);
  }
  root = new node(0, 100000);
  for (int i = 0; i < MAXN; ++i)
    if ((int)V[i].size()) {
      pq.push(make_pair((int)V[i].size(), i));
      sort(V[i].begin(), V[i].end());
      reverse(V[i].begin(), V[i].end());
      for (auto t : V[i]) {
        root->ins(t, 1);
      }
    }
  int ans = 1e9;
  int runcst = 0;
  for (int votes = N; votes > 0; --votes) {
    while ((int)pq.size() && pq.top().first > votes - 1) {
      pair<long long, long long> t = pq.top();
      pq.pop();
      root->ins(V[t.second].back(), -1);
      runcst += V[t.second].back();
      V[t.second].pop_back();
      ++R;
      if ((int)V[t.second].size())
        pq.push(make_pair((int)V[t.second].size(), t.second));
    }
    int needs = votes - R;
    int r = runcst;
    if (needs) r += root->ask(needs);
    ans = min(ans, r);
  }
  cout << ans;
}
