Optimal Tournament

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 MB

Problem

You are preparing the bracket of an annual programming contest. This year NN contestants take part, and the contest runs as a knockout tournament.

A tournament is a binary tree with NN 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 ii is the integer AiA_i. 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 AiA_i and a contestant of strength AjA_j is the difference of the two strengths, AiAj|A_i - A_j|. 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 KK. 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.

Input

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 NN and KK (2N10002 \le N \le 1000, 1K501 \le K \le 50). It is guaranteed that N2KN \le 2^K.

The second line contains the strengths A1,A2,,ANA_1, A_2, \dots, A_N (1Ai1000001 \le A_i \le 100000).

Output

Print the smallest boringness among all tournaments of height at most KK.