#P8746. [蓝桥杯 2021 省 A] 分果果

[蓝桥杯 2021 省 A] 分果果

Problem Description

Xiao Lan wants to distribute nn packs of candies to mm 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 11 to nn, and the ii-th pack has weight wiw_{i}. The children are numbered from 11 to mm. 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 6,1,2,7,96,1,2,7,9. If he wants to distribute them to 2 children, he can buy an extra copy of every pack. Then both children can receive packs 11 to 55, and each gets total weight 2525, so the difference is 00.

As another example, Xiao Lan has 5 packs of candies with weights 6,1,2,7,96,1,2,7,9. If he wants to distribute them to 3 children, he can buy an extra copy of pack 33. The first child receives packs 11 to 33, the second receives packs 33 to 44, and the third receives pack 55. Each child gets total weight 99, so the difference is 00.

As another example, Xiao Lan has 5 packs of candies with weights 6,1,2,7,96,1,2,7,9. If he wants to distribute them to 4 children, he can buy an extra copy of packs 33 and 55, and he still can make each child receive total weight 99, so the difference is 00.

As another example, Xiao Lan has 5 packs of candies with weights 6,1,2,7,96,1,2,7,9. If he wants to distribute them to 5 children, he can buy an extra copy of packs 44 and 55. The first child receives packs 11 to 22 with total weight 77, the second child receives packs 33 to 44 with total weight 99, the third child receives pack 44 with total weight 77, and the fourth and fifth children both receive pack 55 with total weight 99. The difference is 22.

Input Format

The first line contains two integers nn and mm, representing the number of candy packs and the number of children.

The second line contains nn integers w1,w2,,wnw_{1}, w_{2}, \cdots, w_{n}, 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 30%30\% of the testdata, $1 \leq n \leq 10, 1 \leq m \leq 10, 1 \leq w_{i} \leq 10$.

For 60%60\% 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, wiw_{i} are generated randomly and uniformly within some interval.

Lanqiao Cup 2021, Round 1, Provincial Contest, Group A, Problem J.

Translated by ChatGPT 5