Paper Rolls
InterviewTime limit1sMemory limit128 MB
Set every roll to one shared unrolled length within each total length using the fewest 1-cm moves.
Problem
There are toilet paper rolls in a bathroom. The -th roll has a total length and a currently unrolled length , and the unrolled length can never exceed the total length (that is, ).
In one move you may roll up or unroll exactly 1 cm of any single roll; in other words, one move increases or decreases some by , and must still hold afterwards. Determine the minimum number of moves needed to make every roll have the same unrolled length.
Input
The first line contains the number of rolls (). Each of the next lines describes one roll with two integers and , the total length and the unrolled length of the -th roll ().
Output
Print a single integer: the minimum number of moves needed to make the unrolled lengths of all rolls equal.