Submit | All submissions | Best solutions | Back to list |
RESTACK - Restacking haybales 2012 |
Farmer John has just ordered a large number of bales of hay. He would like to organize these into N piles (1 ≤ N ≤ 100,000) arranged in a circle, where pile i contains Bi bales of hay. Unfortunately, the truck driver delivering the hay was not listening carefully when Farmer John provided this information, and only remembered to leave the hay in N piles arranged in a circle. After delivery, Farmer John notes that pile i contains Ai bales of hay. Of course, the Ai's and the Bi's have the same sum.
Farmer John would like to move the bales of hay from their current configuration (described by the Ai's) into his desired target configuration (described by the Bi's). It takes him x units of work to move one hay bale from one pile to a pile that is x steps away around the circle. Please help him compute the minimum amount of work he will need to spend.
Input
- Line 1: The single integer N.
- Lines 2..1+N: Line i+1 contains the two integers Ai and Bi (1 ≤ Ai, Bi ≤ 1000).
Output
A single line containing one number, answer to the problem.
Example
Input: 4 7 1 3 4 9 2 1 13 Output: 13
Added by: | Ikhaduri |
Date: | 2012-03-18 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | USACO MAR12 |