Banner Home Page DIY Contests Problems Ranklist Status Statistics
希望用KM来做,有些题目是可以用费用流的请也用KM刷一遍

Chocolate

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 18   Accepted Submission(s) : 8

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

Lethe loves eating chocolates very much. In Lethe's birthday, her good friend echo brings N boxes to her, and makes the boxes on the circle. Furthermore, echo tells Lethe that there are many chocolates in the boxes, but the total number of chocolates doesn't exceed N. Also, echo wants Lethe to displace the chocolates in such a way that in each box remains no more than one chocolate. In one move she can shift one chocolate from current box to its neighboring box. (Each box has two neighboring boxes). Can you tell Lethe the minimum number of move to achieve this goal?

Input

There are multi-cases (The total number of cases won't exceed 20). First line is an integer N(1<=N<=500), the total number of boxes. Then N lines follow, each line includes a number, indicates the number of chocolates in the box.

Output

Output the minimum number of move.

Sample Input

10
1
3
3
0
0
2
0
0
0
0

Sample Output

9

Source

HDU 8th Programming Contest Site(2)

Statistic | Submit | Back