#P1031. [NOIP 2002 提高组] 均分纸牌
[NOIP 2002 提高组] 均分纸牌
Description
There are piles of cards, numbered . Each pile has some cards, and the total number of cards is a multiple of . You may take any number of cards from any pile and move them.
The moving rules are: cards taken from pile can only be moved to pile ; cards taken from pile can only be moved to pile ; 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 , the numbers of cards in the piles are .
It can be done in moves:
- Take cards from the third pile and put them onto the fourth pile; the piles become .
- Take cards from the third pile and put them onto the second pile; the piles become .
- Take card from the second pile and put it onto the first pile; the piles become .
Input Format
The first line contains an integer , the number of piles.
The second line contains integers , 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 of the testdata, , .
Problem Source: NOIP 2002 Senior, Problem 1.
Translated by ChatGPT 5
京公网安备 11011102002149号