Farmer John has N cows that need to be milked. Milking one cow takes exactly one unit of time.
The cows are impatient, so a cow refuses to be milked if Farmer John waits too long. Cow i gives gi gallons of milk, but only if she is milked before her deadline at time di. Time starts at t=0, so at most x cows in total can be milked before time x. In other words, a cow with deadline di has to take one of the first di slots.
Find the largest amount of milk Farmer John can get if he milks the cows in the best possible order.
The first line contains the number of cows N. (1≤N≤10000)
Each of the next N lines contains two integers separated by a space: the gallons gi and the deadline di of cow i. (1≤gi≤1000, 1≤di≤10000)
Print the maximum number of gallons of milk Farmer John can obtain, on one line.