#P1031. [NOIP 2002 提高组] 均分纸牌

[NOIP 2002 提高组] 均分纸牌

Description

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

The moving rules are: cards taken from pile 11 can only be moved to pile 22; cards taken from pile NN can only be moved to pile N1N-1; cards taken from any other pile can be moved to the adjacent pile on the left or right.

Find a way to move the cards so that all piles contain the same number of cards, using the minimum number of moves.

For example, when N=4N=4, the numbers of cards in the 44 piles are 9,8,17,69,8,17,6.

It can be done in 33 moves:

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

Input Format

The first line contains an integer NN, the number of piles.
The second line contains NN integers A1,A2,,ANA_1,A_2,\ldots,A_N, the initial number of cards in each pile.

Output Format

Output a single line: the minimum number of moves needed to make all piles equal.

4
9 8 17 6

3

Hint

Constraints: For 100%100\% of the testdata, 1N1001 \le N \le 100, 1Ai100001 \le A_i \le 10000.

Problem Source: NOIP 2002 Senior, Problem 1.

Translated by ChatGPT 5