luogu#P5120. [USACO18DEC] Convention II S

[USACO18DEC] Convention II S

Problem Description

Although Farmer John was delayed for quite a long time picking up arrivals at the airport, the convention he is holding for his grass-loving cows has been going very smoothly so far. The convention has attracted cows from all over the world.

However, the highlight of the convention now seems to have caused Farmer John some new scheduling trouble. A very small pasture on his farm produces a type of grass that, according to some knowledgeable cows, is the most delicious in the world. Therefore, all NN cows (1N1051\le N\le 10^5) attending the convention want to taste this grass. Since the pasture is so small that it can only hold one cow, this may cause a long line.

Farmer John knows the time aia_i when each cow ii plans to arrive at this special pasture, and the amount of time tit_i she plans to spend tasting the grass when it is her turn. When cow ii starts eating, she will spend the full tit_i time before leaving, and any other cows that have arrived must wait in line. If multiple cows are waiting when the pasture becomes free, then the most senior cow will be the next to taste the fresh grass. Here, a cow that arrives exactly when another cow finishes eating and leaves is considered to be "waiting." Similarly, if multiple cows arrive at the same time when no cow is eating, then the most senior cow is the next one to eat.

Please help FJ compute the maximum waiting time among all cows (the time from aia_i until that cow starts eating).

Input Format

The first line of input contains NN. The next NN lines give the information of the NN cows in order of seniority (the most senior cow is listed first). Each line contains aia_i and tit_i for one cow. All tit_i are positive integers not exceeding 10410^4, and all aia_i are positive integers not exceeding 10910^9.

Output Format

Output the longest waiting time among all cows.

5
25 3
105 30
20 50
10 17
100 10
10

Hint

In this example, we have 55 cows (numbered 151\dots 5 in the order of the input). Cow 44 arrives first (at time 1010). Before she finishes eating (at time 2727), cows 11 and 33 both arrive. Since cow 11 is more senior, she eats first, and she waits a total of 22 time units from her arrival. She finishes eating at time 3030, then cow 33 starts eating, and she waits a total of 1010 time units from her arrival. After a period when no cow is eating, cow 55 arrives. While she is eating, cow 22 also arrives, and can eat after 55 time units. The cow that waits the longest is cow 33.

Translated by ChatGPT 5