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.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author AlexFetisov
 */
public class elections_af {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskD {

    final int MAXN = 100001;

    int[] forWhom;
    int[] priceToChange;
    int[] votesForCandidate;

    int forMe;

    class Vote implements Comparable<Vote> {
        int price;
        int id;

        boolean mark = false;

        Vote(int price, int id) {
            this.price = price;
            this.id = id;
        }


        public int compareTo(Vote o) {
            if (price != o.price) return price - o.price;
            return id - o.id;
        }
    }

    List<Vote> votes;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        votesForCandidate = new int[MAXN];
        forMe = 0;
        forWhom = new int[n];
        votes = new ArrayList<Vote>();
        priceToChange = new int[n];

        for (int i = 0; i < n; ++i) {
            forWhom[i] = in.nextInt();
            priceToChange[i] = in.nextInt();
            if (forWhom[i] == 0) {
                ++forMe;
            } else {
                votesForCandidate[forWhom[i]]++;
                votes.add(new Vote(priceToChange[i], forWhom[i]));
            }
        }
        Collections.sort(votes);
        long res = Long.MAX_VALUE;
        int S = 1, F = n;
        while (S + 10 <= F) {
            int delta = (F - S) / 3;
            int e1 = S + delta;
            int e2 = F - delta;
            long f1 = f(e1);
            long f2 = f(e2);
            if (f1 < f2) {
                F = e2;
            } else {
                S = e1;
            }
        }
        for (int i = S; i <= F; ++i) {
            res = Math.min(res, f(i));
        }
        out.println(res);
    }

    private long f(int needVotes) {
        int curForMe = forMe;
        long res = 0;
        int[] curVotes = new int[MAXN];
        for (Vote v : votes) {
            v.mark = false;
        }
        System.arraycopy(votesForCandidate, 1, curVotes, 1, MAXN - 1);
        for (Vote v : votes) {
            if (curVotes[v.id] >= needVotes) {
                res += v.price;
                v.mark = true;
                ++curForMe;
                curVotes[v.id]--;
            }
        }
        int need = needVotes - curForMe;
        if (need > 0) {
            for (Vote v : votes) {
                if (!v.mark) {
                    --need;
                    res += v.price;
                    if (need == 0) break;
                }
            }
        }
        return res;
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer stt;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
    }

    public String nextLine() {
        try {
            return reader.readLine().trim();
        } catch (IOException e) {
            return null;
        }
    }

    public String nextString() {
        while (stt == null || !stt.hasMoreTokens()) {
            stt = new StringTokenizer(nextLine());
        }
        return stt.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

}
