#t363. 均分纸牌
均分纸牌
Dividing Cards Equally
Problem Description
There are piles of cards, numbered . Each pile has some number of cards, but the total number of cards must be a multiple of . You may take some cards from any pile and then move them.
The moving rule: cards taken from pile may only be moved to pile ; cards taken from pile may only be moved to pile ; 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 , the piles have cards respectively.
It takes moves to reach the goal:
- Take cards from the third pile and put them on the fourth pile; now the piles are .
- Take cards from the third pile and put them on the second pile; now the piles are .
- Take card from the second pile and put it on the first pile; now the piles are .
Input Format
The first line is a single integer , the number of piles. The second line has integers , 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 of the data, , .
【题目来源】 NOIP 2002 Advanced Group, Problem 1.