#t363. 均分纸牌

均分纸牌

Dividing Cards Equally

Problem Description

There are NN piles of cards, numbered 1,2,…,N1, 2, \ldots, N. Each pile has some number of cards, but the total number of cards must be a multiple of NN. You may take some cards from any pile and then move them.

The moving rule: cards taken from pile 11 may only be moved to pile 22; cards taken from pile NN may only be moved to pile N−1N-1; cards taken from any other pile may be moved to the adjacent pile on the left or on the right.

Now find a moving method that makes the number of cards in each pile equal, using the fewest moves.

For example, when N=4N=4, the 44 piles have 9,8,17,69, 8, 17, 6 cards respectively.

It takes 33 moves to reach the goal:

  • Take 44 cards from the third pile and put them on the fourth pile; now the piles are 9,8,13,109, 8, 13, 10.
  • Take 33 cards from the third pile and put them on the second pile; now the piles are 9,11,10,109, 11, 10, 10.
  • Take 11 card from the second pile and put it on the first pile; now the piles are 10,10,10,1010, 10, 10, 10.

Input Format

The first line is a single integer NN, the number of piles. The second line has NN integers A1,A2,…,ANA_1, A_2, \ldots, A_N, the initial number of cards in each pile.

Output Format

One line, the minimum number of moves needed for all piles to become equal.

Sample Input

4
9 8 17 6

Sample Output

3

Hint

For 100%100\% of the data, 1≤N≤1001 \le N \le 100, 1≤Ai≤100001 \le A_i \le 10000.

【题目来源】 NOIP 2002 Advanced Group, Problem 1.