#P3103. [USACO14FEB] Airplane Broading G

[USACO14FEB] Airplane Broading G

题目描述

FJ's cows have decided to take a vacation, and have miraculously managed to find an airline willing to sell them tickets. When they arrive at the airport and start boarding their plane, they face an interesting problem, however.

The airplane has NN seats, which we model as the points x=1x=1 through x=Nx=N on the number line. All N cows (1N200,000)(1 \le N \le 200,000) 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 so on. Cow i has been assigned to Seat SiS_i, where S1S_1,...,SNS_N is a permutation of 11,...,NN.

At each time step, each cow takes a step to the right if she can. When cow ii reaches her seat SiS_i, she will stop to put her baggage in the overhead bin, which takes TiT_i seconds, and then sit down. For those TiT_i steps, the cow behind her (if there is one) is blocked from moving forward. If there is a line of cows behind her, the line is effectively blocked as well.

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

The sum of TiT_i for all cows will be less than 1,000,000,0001,000,000,000.

输入格式

  • Line 11: A single integer, NN.

  • Lines 22..N+1N+1: Two space-separated integers, SiS_i and TiT_i.

输出格式

  • Line 11: A single line indicating the amount of time it takes to seat all cows.
3 
2 5 
3 10 
1 5 

19 

提示

Initially, the cows are situated like this:

cows -> 123123

123123 <- seats

with cow 11 trying to get to seat 22, cow 22 trying to get to seat 33, and cow 33 trying to get to seat 11.

After one step, they will all move 11 to the right and cow 33 will reach her seat:

123 123 Cow 33 takes 55 seconds to sit down, at which point she effectively disappears.

12 123 It takes 33 more seconds for cows 11 and 22 to reach their desired seats:

12 123 It takes 55 seconds for cow 11 to sit down and 1010 seconds for cow 22 to sit down, so that's 1010 seconds total.

In total this took 1+5+3+10=191 + 5 + 3 + 10 = 19 seconds.