#P5817. [CQOI2011] 分金币

[CQOI2011] 分金币

Description

There are nn people sitting around a round table. Each person has some number of gold coins, and the total number of coins is divisible by nn. Each person can give some coins to their left and right neighbors, and in the end everyone must have the same number of coins.

Your task is to find the minimum total number of coins that are passed from hand to hand.

Input Format

The first line contains an integer nn (n3n \ge 3).

The next nn lines each contain a positive integer, given in counterclockwise order, representing the number of coins each person has.

Output Format

Output one line containing the minimum total number of coins that are passed from hand to hand.

4
1
2
5
4

4

Hint

Sample Explanation

Suppose the four people are numbered 1,2,3,41,2,3,4. Person 33 gives person 22 22 coins (becoming 1,4,3,41,4,3,4). Then person 22 and person 44 each give person 11 11 coin.

Constraints

For 100%100\% of the testdata, 3n1053 \le n \le 10^5, and totaltotal coins109coins \le 10^9.

Translated by ChatGPT 5