This page is still under construction.

Parts of this page are still being built. What you see may change.

Fast Food

Interview

Time limit1sMemory limit128 MB

Summary
Given sorted restaurant positions, choose k of them as depots to minimize the total distance from every restaurant to its nearest depot.
Level

Medium7 of 10

Topics
Dynamic programming, Divide and conquer, Binary search, Greedy
Solved
No attempts yet

Problem

The fast-food chain McBurger owns several restaurants along a highway and wants to build several depots, each located at one of its restaurants, to supply the others with ingredients. The depots must be placed so that the total distance between each restaurant and its assigned depot is minimized.

You are given the positions of nn restaurants along the highway as integers d1<d2<⋯<dnd_1 < d_2 < \dots < d_n (distances from a fixed reference point on the same highway), together with a number kk (k≤nk \le n), the number of depots to build.

The kk depots are built at the locations of kk distinct restaurants, and each restaurant is served by the closest depot. The total distance sum

∑i=1n∣di−p(i)∣\sum_{i=1}^{n} \left| d_i - p(i) \right|

must be as small as possible, where p(i)p(i) is the position of the depot serving restaurant ii. Compute the minimum possible total distance sum.

Input

The input contains several fast-food chains. Each chain starts with a line containing two integers nn and kk with 1≤n≤2001 \le n \le 200, 1≤k≤301 \le k \le 30, and k≤nk \le n. The next nn lines each contain one integer, the positions did_i of the restaurants in increasing order.

The input ends with a chain whose first line is 0 0; this chain is not processed.

Output

For each chain, in the order given, print one line Chain i: S, where i is the chain's 1-based index and S is the minimum possible total distance sum. The terminating 0 0 chain produces no output.

Examples2

  1. Example 1

    Input
    6 3
    5
    6
    12
    19
    20
    27
    0 0
    
    Expected output
    Chain 1: 8
    
  2. Example 2

    Input
    3 1
    1
    2
    3
    2 1
    10
    20
    0 0
    
    Expected output
    Chain 1: 2
    Chain 2: 10