401368: GYM100418 H Equalize

Memory Limit:64 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

H. Equalizetime limit per test1 secondmemory limit per test64 megabytesinputstandard inputoutputstandard output

One of the most important responsibilities of an officer in the army is finding a task for soldiers. So, a brave platoon got an important mission: to group logs into equal piles. Logs have already been factored, but, unfortunately, the numbers of logs in the piles were different. The execution of this order should not take less than an hour, that is why the officer gave the next restrictions: in one minute they can take exactly one log and shift it only to a neighboring pile. Help soldiers to determine how many minutes they need to equalize all the piles of logs.

Input

The first line contains the number N. The next N lines contain the pairs of integer numbers Ki, Ai (1 ≤ N, Ki ≤ 105, 0 ≤ Ai ≤ 105). It means that Ki piles in a row have Ai logs. Each of the first K1 groups have A1 logs, after that K2 groups have A2 logs and so on.

Output

Output the smallest number of minutes the platoon needs to perform the task. Because the answer may be too large, you should output the answer modulo 109 + 7. It's guaranteed that the answer exists always.

ExamplesInput
3
4 8
3 1
2 5
Output
42

加入题单

算法标签: