#P8746. [蓝桥杯 2021 省 A] 分果果
[蓝桥杯 2021 省 A] 分果果
Problem Description
Xiao Lan wants to distribute packs of candies to children at his birthday party. Every pack must be given out, and each child must receive at least one pack, possibly more.
Xiao Lan has prepared the candies in advance. To make the distribution more even on the party day, he wants to compute a plan beforehand.
He numbers the candy packs from to , and the -th pack has weight . The children are numbered from to . Each child can only receive candies whose indices form a consecutive segment. Xiao Lan has thought for a long time but still cannot find a suitable plan so that every child gets roughly the same total weight. Therefore, he needs your help. To distribute better, he can buy some extra candies so that certain indexed packs have two copies. When a pack with some index has two copies, any single child can receive at most one of the two copies.
Find a plan that minimizes the difference between the maximum total weight and the minimum total weight among all children, and output this difference.
For example, Xiao Lan has 5 packs of candies with weights . If he wants to distribute them to 2 children, he can buy an extra copy of every pack. Then both children can receive packs to , and each gets total weight , so the difference is .
As another example, Xiao Lan has 5 packs of candies with weights . If he wants to distribute them to 3 children, he can buy an extra copy of pack . The first child receives packs to , the second receives packs to , and the third receives pack . Each child gets total weight , so the difference is .
As another example, Xiao Lan has 5 packs of candies with weights . If he wants to distribute them to 4 children, he can buy an extra copy of packs and , and he still can make each child receive total weight , so the difference is .
As another example, Xiao Lan has 5 packs of candies with weights . If he wants to distribute them to 5 children, he can buy an extra copy of packs and . The first child receives packs to with total weight , the second child receives packs to with total weight , the third child receives pack with total weight , and the fourth and fifth children both receive pack with total weight . The difference is .
Input Format
The first line contains two integers and , representing the number of candy packs and the number of children.
The second line contains integers , representing the weight of each pack.
Output Format
Output one integer, representing the difference between the maximum total weight and the minimum total weight among the children in the optimal plan.
5 2
6 1 2 7 9
0
5 5
6 1 2 7 9
2
Hint
For of the testdata, $1 \leq n \leq 10, 1 \leq m \leq 10, 1 \leq w_{i} \leq 10$.
For of the testdata, $1 \leq n \leq 30, 1 \leq m \leq 20, 1 \leq w_{i} \leq 30$.
For all testdata, $1 \leq n \leq 100, 1 \leq m \leq 50, 1 \leq w_{i} \leq 100$. In the testdata, are generated randomly and uniformly within some interval.
Lanqiao Cup 2021, Round 1, Provincial Contest, Group A, Problem J.
Translated by ChatGPT 5