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
import java.util.*;
import java.io.*;

public class C {
  static int CANDS = 100*1000+1;

  static int n;
  static int[][] A;

  static void check(boolean b) {
    if(!b) { throw new RuntimeException(); }
  }

  static Comparator<Integer> byCost = new Comparator<Integer>() {
    public int compare(Integer a, Integer b) {
      return A[a][1] - A[b][1];
    }
  };

  static class TheirVotes {
    static TreeMap<Integer, List<Integer>> BY_VOTES;
    static List<LinkedList<Integer>> VOTES;

    public TheirVotes() {
      VOTES = new ArrayList<LinkedList<Integer>>();
      for(int i=0; i<CANDS; i++) {
        VOTES.add(new LinkedList<Integer>());
      }
      for(int i=0; i<n; i++) {
        VOTES.get(A[i][0]).add(i);
      }
      for(int i=0; i<CANDS; i++) {
        Collections.sort(VOTES.get(i), byCost);
      }

      BY_VOTES = new TreeMap<Integer, List<Integer>>();
      for(int i=0; i<CANDS; i++) {
        int sz = VOTES.get(i).size();
        if(!BY_VOTES.containsKey(sz)) { BY_VOTES.put(sz, new ArrayList<Integer>()); }
        BY_VOTES.get(sz).add(i);
      }
    }

    List<Integer> remove_one() {
      List<Integer> ans = new ArrayList<Integer>();

      Map.Entry<Integer, List<Integer>> top = BY_VOTES.pollLastEntry();
      check(top.getKey() >= 0);
      for(int ci : top.getValue()) {
        int vi = VOTES.get(ci).pollFirst();
        ans.add(vi);
      }
      int newkey = top.getKey() - 1;
      if(!BY_VOTES.containsKey(newkey)) { BY_VOTES.put(newkey, top.getValue()); }
      else { BY_VOTES.get(newkey).addAll(top.getValue()); }

      return ans;
    }

    boolean done() { return BY_VOTES.lastKey() == 0; }
  }

  static class CheapVotes {
    TreeMap<Long, Integer> X;
    long mycost;
    int sz;

    void add(long x) {
      X.put(x, X.containsKey(x) ? X.get(x)+1 : 1);
      mycost += x;
      sz++;
    }
    void remove(long x) {
      Integer cnt = X.get(x);
      if(cnt == null) {
      } else if(cnt==1) {
        X.remove(x);
        mycost -= x;
        sz--;
      } else {
        X.put(x, cnt-1);
        mycost -= x;
        sz--;
      }
    }

    public CheapVotes() {
      int[] SCORE = new int[CANDS];
      for(int i=0; i<n; i++) {
        SCORE[A[i][0]]++;
      }
      int their_score = 0;
      for(int i=1; i<CANDS; i++) {
        their_score = Math.max(their_score, SCORE[i]);
      }
      int want = their_score+1 - SCORE[0];

      ArrayList<Integer> COST = new ArrayList<Integer>();
      for(int i=0; i<n; i++) {
        if(A[i][0] != 0) {
          COST.add(A[i][1]);
        }
      }
      Collections.sort(COST);

      X = new TreeMap<Long, Integer>();
      for(int i=0; i<n && sz<want; i++) {
        add(COST.get(i));
      }
      while(sz < want) {
        add(1000L*1000*100);
      }
    }

    boolean done() { return X.size()==0; }

    long cost() {
      return mycost;
    }

    void buy(int vi) {
      long cost = A[vi][1];
      if(X.containsKey(cost)) {
        remove(cost);
      } else {
        remove_max();
      }
    }
    void remove_max() {
      if(X.size()==0) { return; }
      remove(X.lastKey());
    }
  }

  public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    n = Integer.parseInt(in.readLine());
    A = new int[n][2];
    for(int i=0; i<n; i++) {
      String[] arr = in.readLine().split(" ");
      A[i][0] = Integer.parseInt(arr[0]);
      A[i][1] = Integer.parseInt(arr[1]);
    }

    CheapVotes cheap = new CheapVotes();
    TheirVotes their = new TheirVotes();

    long ans = 1000*1000*1000;

    long paid = 0;
    while(true) {
//      System.out.printf("paid=%d cheap.cost=%d\n", paid, cheap.cost());
      ans = Math.min(ans, paid + cheap.cost());
      if(their.done() || cheap.done()) { break; }
      List<Integer> voters = their.remove_one();
      cheap.remove_max();
      for(Integer vi : voters) {
//        System.out.printf("BOUGHT %d\n", vi);
        paid += A[vi][1];
        cheap.buy(vi);
      }
    }

    System.out.println(ans);
  }
}
