Low Power
Time limit4sMemory limit256 MB
Sort 2nk batteries into groups of k and pair the group minima into n machines to minimize the largest pair gap.
- Level
Medium6 of 10
- Topics
- Binary search, Greedy, Sorting
- Solved
- No attempts yet
Problem
You are building advanced chips for machines. The chips themselves are easy to make, but powering them is tricky because the available batteries have widely varying power outputs.
There are machines. Each machine has two chips, and each chip is powered by exactly batteries. The absolute amount of power a chip receives does not matter; a machine works best when its two chips have power outputs that are as close as possible. The power output of a chip is defined as the smallest power output among its batteries.
You must distribute all batteries among the chips (every battery is used, and each chip receives exactly of them). It may be impossible to make both chips of every machine have equal power output, so instead you want to guarantee that in every machine the two chips' power outputs differ by at most , and you want to be as small as possible. Find that smallest possible .
For example, suppose there are machines with batteries per chip and batteries with power outputs . One valid distribution assigns powers to one chip, to the other chip of the same machine, to the third chip, and to the fourth. The four chip power outputs are then , so both machines have a difference of . Many other distributions achieve the same result.
Input
The input is a single test case. The first line contains two positive integers: the number of machines and the number of batteries per chip (with ). The second line contains integers , the power outputs of the batteries (with ).
Output
Print the smallest number such that the batteries can be distributed so that, in every machine, the two chips' power outputs differ by at most .