Bob came to a cash & carry store, put n items into his trolley, and went to the checkout counter to pay. Each item is described by its price ci and time ti in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other items from his trolley. To steal one item Bob needs exactly 1 second. What is the minimum amount of money that Bob will have to pay to the checkout assistant? Remember, please, that it is Bob, who determines the order of items for the checkout assistant.

Input

The first input line contains number n (1 ≤ n ≤ 2000). In each of the following n lines each item is described by a pair of numbers ti, ci (0 ≤ ti ≤ 2000, 1 ≤ ci ≤ 109). If ti is 0, Bob won't be able to steal anything, while the checkout assistant is occupied with item i.

Output

Output one number — answer to the problem: what is the minimum amount of money that Bob will have to pay.

Examples

Input

4
2 10
0 20
1 5
1 3


Output

8


Input

3
0 1
0 10
0 100


Output

111
import java.util.PriorityQueue;
import java.util.Scanner;

public class CheckoutAssistant {

    public void solve() {
        Scanner scanner = new Scanner(System.in);
        int itemCount = scanner.nextInt();
        boolean hasTimeItem = false;
        PriorityQueue<Item> costTimeItems = new PriorityQueue<Item>();
        
        PriorityQueue<Item> zeroTimeItems = new PriorityQueue<Item>();
        
        for (int i = 0; i < itemCount; i++) {
            int time = scanner.nextInt() + 1;
            Long price = scanner.nextLong();
            Item item = new Item(time, price);
            
            if(time == 1){
                zeroTimeItems.offer(item);
            }
            else{
                costTimeItems.offer(item);
                hasTimeItem = true;
            }
            
        }// end for
        
        if(hasTimeItem){
            handleTimeCostItems(itemCount, costTimeItems, zeroTimeItems);
        }
        else{
            int sumCost = 0;
            while(zeroTimeItems.peek() != null)
                sumCost += zeroTimeItems.poll().getPrice();
            System.out.println(sumCost);
        }

    }

    public void handleTimeCostItems(int itemCount, PriorityQueue<Item> costTimeItems, PriorityQueue<Item> zeroTimeItems){
        Long[] resultPrice = new Long[itemCount];
        int[] resultTime = new int[itemCount];
        int sumTime = 0;

        Item firstItem = costTimeItems.poll();
        resultPrice[0] = firstItem.getPrice();
        resultTime[0] = firstItem.getTime();
        sumTime = firstItem.getTime();

        int count = 1;
        Item item = null;

        while (sumTime < itemCount && costTimeItems.peek() != null) {
            item = costTimeItems.poll();
            resultPrice[count] = item.getPrice();
            resultTime[count++] = item.getTime();
            sumTime += item.getTime();
        }

        if (sumTime == itemCount) {
            replace(count, costTimeItems, sumTime, resultTime, resultPrice, itemCount);
        }
        else if(sumTime > itemCount && count >= 2){
            int timeFirst = resultTime[count - 2];
            if(sumTime - timeFirst == itemCount){
                sumTime -= timeFirst;
                resultPrice[count - 2] = resultPrice[count - 1];
                resultTime[count - 2] = resultTime[count - 1];
                count--;
            }
            replace(count, costTimeItems, sumTime, resultTime, resultPrice, itemCount);
        }// end else
        else if(sumTime > itemCount){
            print(resultPrice, count);
        }
        
        else if(sumTime < itemCount){//sumTime < itemCount
            int leftCount = sumTime - count;
            while(leftCount > 0){
                resultPrice[count++] = zeroTimeItems.poll().getPrice();
                leftCount--;
            }//end while
            print(resultPrice, count);
        }
    }
    
    public void replace(int count, PriorityQueue<Item> costTimeItems, int sumTime, int[] resultTime, Long[] resultPrice, int itemCount){
        Item item = null;
        while (count > 1 && costTimeItems.peek() != null) {
            item = costTimeItems.poll();

            if (sumTime - resultTime[count - 1] - resultTime[count - 2]
                    + item.getTime() >= itemCount
                    && item.getPrice() < resultPrice[count - 1]
                            + resultPrice[count - 2]) {
                resultPrice[count - 1] = 0L;
                resultPrice[count - 2] = item.getPrice();
                count--;
            }//end if
        }//end while
        print(resultPrice, count);
    }
    
    public void print(Long[] resultPrice, int count) {
        Long sumPrice = 0L;
        for (int i = 0; i < count; i++)
            sumPrice += resultPrice[i];
        System.out.println(sumPrice);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CheckoutAssistant ca = new CheckoutAssistant();
        ca.solve();
    }

}

class Item implements Comparable<Item> {
    private int time;
    private Long price;

    public Item(int time, Long price) {
        this.time = time;
        this.price = price;
    }

    @Override
    public int compareTo(Item i) {
        // TODO Auto-generated method stub
        if(this.price - i.price > 0)
            return 1;
        else if(this.price - i.price < 0)
            return -1;
        else
            return 0;
    }

    public int getTime() {
        return time;
    }

    public Long getPrice() {
        return price;
    }
}