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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Two {
	InputStreamReader inp = new InputStreamReader(System.in);
	BufferedReader in = new BufferedReader(inp);
	boolean test = true;

//	private String[] inData = { "4","2 10","0 20","1 5","1 3" };
	private String[] inData = { "4",
			"2 1",
			"0 10",
			"1 100000000",
			"0 100"};	

	static int id = -1;

	public String readLine() {
		id++;
		if (test)
			return inData[id];
		else
			try{
				return in.readLine();	
			} catch( Exception e){
				e.printStackTrace();
			}
		return "";
	}
	
	public Integer nextInt(){
		return Integer.valueOf(readLine());
	}
	
	class Item{
		int price;
		int time;
		public Item(int time, int price) {
			super();
			this.price = price;
			this.time = time;
		}
		@Override
		public String toString() {
			return "Item [price=" + price + ", time=" + time + "]";
		}
	}

	public void solve() throws Exception {

		int nrOfItems = nextInt();
		
		List<Item> items = new ArrayList<Item>();
		
		for (int i = 0; i < nrOfItems; i++) {
			String line = readLine();
			
			items.add(new Item(Integer.valueOf(line.split(" ")[0]), Integer.valueOf(line.split(" ")[1])));
		}
		
		Collections.sort(items, new Comparator<Item>() { //sort to get good answears early

			@Override
			public int compare(Item o1, Item o2) {
				if(o1.price > o2.price)
					return 1;
				if(o1.price < o2.price)
					return -1;
				if(o1.time > o1.time)
					return -1;
				if(o1.time < o1.time)
					return 1;
				return 0;
			}
		});
		
		long totMoney = getBestTry(items, new ArrayList<Integer>(), Long.MAX_VALUE); 

		System.out.println(totMoney);
	}
	
	private long getBestTry(List<Item> items, List<Integer> payIds, long bestVal){
		
		bestVal = getMoneyWithCurrentOrder(items, payIds, bestVal);
		
		long sumOfTimes = 0;
		long sumOfPrices = 0;
		
		for (Integer integer : payIds) {
			sumOfTimes += items.get(integer).time;
			sumOfPrices += items.get(integer).price;
		}
		
		if(sumOfTimes > items.size() - payIds.size()
				|| sumOfPrices > bestVal){ // no need to go deeper
			return bestVal;
		}
		
		
		for (int i = 0; i < items.size() ; i++) {
			Integer skipId = Integer.valueOf(i);
			
			if(items.get(skipId).time == 0)
				continue;
			
			if(!payIds.contains(skipId)){
				payIds.add(skipId);
				
				bestVal = getBestTry(items, payIds, bestVal);
				
				payIds.remove(skipId);
			}
		}
		
		return bestVal;
	}
	
	private long getMoneyWithCurrentOrder(List<Item> items, List<Integer> payIds, long bestVal) {
		//start paying for the id's then steal all the most expensive items left
		
		long totMoney = 0;
		long stealTime = 0;
		
		for (Integer integer : payIds) {
			totMoney += items.get(integer).price;
			stealTime += items.get(integer).time;
			
			if(totMoney > bestVal){
				return bestVal;
			}
		}
		
		for (int i = 0; i < items.size(); i++) {
			if(payIds.contains(Integer.valueOf(i))){
				continue;
			}
			if(stealTime > 0){
				stealTime--;
				continue;
			}
			Item item = items.get(i);
			stealTime = item.time;
			totMoney += item.price;
			if(totMoney >= bestVal){
				return bestVal;
			}
		}
		
		if(totMoney >= bestVal){
			return bestVal;
		}
		return totMoney;
	}

	public static void main(String[] args) throws Exception {
//		long currentTimeMillis = System.currentTimeMillis();
		new Two().solve();
//		System.out.println("Time: " + (System.currentTimeMillis() - currentTimeMillis));
		}
}