#C10021. Minimize Chocolate Packet Difference

    ID: 39181 Type: Default 1000ms 256MiB

Minimize Chocolate Packet Difference

Minimize Chocolate Packet Difference

You are given a list of packets, where each packet contains a certain number of chocolates, and an integer m representing the number of children. Each child must receive exactly one packet. Your task is to select m packets such that the difference between the maximum and minimum number of chocolates among the selected packets is minimized.

If it is impossible to distribute the packets (i.e. when the number of children exceeds the number of packets), output -1.

The minimal difference is computed as follows:

\(\min_{0 \le i \le n-m} \{ packets[i+m-1] - packets[i] \}\)

inputFormat

The first line contains an integer n denoting the number of packets. The second line contains n space-separated integers representing the number of chocolates in each packet. The third line contains an integer m representing the number of children.

outputFormat

Print a single integer representing the minimum difference between the maximum and minimum number of chocolates in the chosen packets. If distribution is not possible, print -1.## sample

17
12 4 7 9 2 23 25 41 30 40 28 42 30 44 48 43 50
7
10