-->
当前位置:首页 > 题库

PROGRAMMING:Divide the match equally

Luz5年前 (2021-05-10)题库403
After the match factory produces matches, they should be boxed. There are n matchboxes to be filled with matches. The numbers are   1,2,…,N。 At first, the workers put a number of matches randomly on each pile. However, a batch of matchboxes must be filled with the same number of pieces, and they must be able to be packed into the matchbox. Therefore, it is required to move some matches left and right in any box.
The rules for moving matches are as follows: matches taken from box No. 1 can only be moved to box No. 2; Matches taken from box n can only be moved to box n-1; Other boxes of matches can be moved to the adjacent boxes on the left or right. Now we need to find a way to move, with the least number of times to make each box of firewood are the same number.
For example, n = 4, the number of 4 boxes of matches are:
①9 ② 8 ③ 17 ④ 6
Move 3 times to achieve the goal:
Take 4 matches from ③ and put them on ④ (9 8 13 10) - > take 3 matches from ③ and put them on ② (9 11 10) - > take 1 match from ② and put them on ① (10 10 10).
The answer is required in a second, 128MB of memory.
###Input format:
The input file contains two lines of data.
The first line is the value of N matchboxes (1 < = n < = 100).
The second line is the initial number of matches A1, A2,..., an in n matchboxes (L < = AI < = 10000).
###Output format:
There is only one line in the output file, that is, the minimum number of moves when the number of matches in all boxes is equal.
###Input example:
Here is a set of inputs. For example:
```in
four
9 8 17 6
```
###Output example:
The corresponding output is given here. For example:
```out
three
```







answer:If there is no answer, please comment