#Q11. 「一本通 1.1 练习 6」糖果传递

「一本通 1.1 练习 6」糖果传递

Description

Original source: HAOI 2008

There are nn children sitting in a circle, each with aia_i candies. Each child can only pass candies to their immediate left or right neighbors. The cost of passing one candy is 11. Find the minimum total cost to make all children have an equal number of candies.

Input Format

The first line contains an integer nn, the number of children;

The next nn lines each contain an integer aia_i.

Output Format

Output the minimum total cost to make all children have an equal number of candies.

Sample 1

4
1
2
5
4

4

Constraints & Hints

For 30%30\% of the data, n1000n \leq 1000;

For 100%100\% of the data, n106n \leq 10^6, and it is guaranteed that the answer can be stored in a 64-bit signed integer.