Boxes
InterviewTime limit1sMemory limit128 MB
Boxes stand in a circle with at most n balls total; move balls to neighbors so each box holds at most one, minimizing the number of moves.
- Level
Medium7 of 10
- Topics
- Greedy, Prefix sum, Math, Implementation
- Solved
- No attempts yet
Problem
There are boxes arranged in a circle, numbered from to in clockwise order (). Because the boxes form a circle, box and box are also neighbors. Some boxes contain balls, and the total number of balls is at most .
You want to rearrange the balls so that every box holds at most one ball. In one move you may shift a single ball from a box to one of its two neighboring boxes.
Write a program that computes the minimum number of moves needed so that each box holds at most one ball.
Input
The first line contains the number of boxes . Each of the next lines contains one nonnegative integer; the integer on the -th of these lines is the number of balls in box . The total number of balls is at most .
Output
Print a single nonnegative integer: the minimum number of moves needed so that each box holds at most one ball.