401368: GYM100418 H Equalize
Description
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.
InputThe 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.
OutputOutput 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.
ExamplesInput3Output
4 8
3 1
2 5
42