Optimal Tournament
Time limit5sMemory limit512 MB
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.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Tree, Greedy
- Solved
- No attempts yet
Problem
You are preparing the bracket of an annual programming contest. This year contestants take part, and the contest runs as a knockout tournament.
A tournament is a binary tree with 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 is the integer . 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 and a contestant of strength is the difference of the two strengths, . 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 . 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 and (, ). It is guaranteed that .
The second line contains the strengths ().
Output
Print the smallest boringness among all tournaments of height at most .