Place N contestants with given strengths at the leaves of a knockout bracket of height at most K so that the total strength difference over all matches is minimized.
Hard8Dynamic programmingSortingTreeGreedyNo attempts yetTime limit5sMemory limit512 MBYou are preparing the bracket of an annual programming contest. This year N contestants take part, and the contest runs as a knockout tournament.
A tournament is a binary tree with N leaf nodes, and the contestants are assigned to the leaf nodes one to one. In each match two contestants compete: the winner goes on to the next round and the loser is eliminated. The one contestant who outlasts all the others is the champion, and the final match corresponds to the root node of the tree.
The strength of contestant i is the integer Ai. When two contestants compete, the one with the greater strength always wins. If their strengths are equal, the winner is decided at random.
In past contests the audience complained that too many matches were one sided. The boringness of a match and of a tournament is therefore defined as follows. The boringness of a match between a contestant of strength Ai and a contestant of strength Aj is the difference of the two strengths, ∣Ai−Aj∣. The boringness of a tournament is the sum of the boringness of every match in the tree.
You may use any shape of tournament, including unbalanced ones, as long as the height of the tournament is at most K. The height of a tournament is the maximum number of matches on the simple path from the root node to a leaf node.
Write a program that computes the minimum boringness.

Figure 1. Two possible tournaments for the first example. The height of the left one is 2, and the height of the right one is 3.
The input consists of a single test case in the following format.
N K
A_1 A_2 ... A_N
The first line contains two integers N and K (2≤N≤1000, 1≤K≤50). It is guaranteed that N≤2K.
The second line contains the strengths A1,A2,…,AN (1≤Ai≤100000).
Print the smallest boringness among all tournaments of height at most K.