Airplane Boarding

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John's cows decided to take a vacation, and they managed to find an airline willing to sell them tickets. When they arrive at the airport and start boarding their plane, they run into an interesting problem.

The plane has NN seats, which we model as the points x=1x = 1 through x=Nx = N on the number line. All NN cows are standing in line waiting to get to their seats. Cow NN is at position x=0x = 0, cow N1N-1 is at position x=1x = -1, and the rest line up one step further back in the same way. Cow ii has been assigned seat SiS_i, where S1,,SNS_1, \dots, S_N is a permutation of 11 through NN.

Every second, each cow takes one step to the right if she can. When cow ii reaches her seat SiS_i, she stops to put her baggage in the overhead bin, which takes TiT_i seconds, and then sits down. During those TiT_i seconds the cow right behind her cannot move forward, and if a line of cows has formed behind her, that line is stuck as well.

How long does it take for all the cows to sit down?

NN is between 1 and 200,000, and the sum of all TiT_i is less than 1,000,000,000.

Input

The first line contains the integer NN. The ii-th of the next NN lines contains two space-separated integers SiS_i and TiT_i.

Output

Print on a single line the amount of time it takes to seat all the cows.

Hint

Suppose there are three cows, cow 1 heads for seat 2, cow 2 heads for seat 3, and cow 3 heads for seat 1, with baggage times of 5, 10, and 5 seconds. At the start cow 3 stands at x=0x = 0, cow 2 at x=1x = -1, and cow 1 at x=2x = -2.

After one second all three move one step to the right and cow 3 reaches her seat. She needs 5 seconds to stow her baggage and sit down, and the two cows behind her stay put that whole time. Once she sits down she is effectively gone from the aisle.

Over the next 3 seconds cow 1 and cow 2 each reach their own seat. Cow 1 needs 5 seconds to sit down and cow 2 needs 10, and they stow their baggage at the same time, so 10 more seconds pass. The whole thing takes 1+5+3+10=191 + 5 + 3 + 10 = 19 seconds.