TripleCorn

TripleCorn is a company that produces corn cobs triple as big and double as tasty as any other company. Toby, the cute, small and smart dog, has been hired by Triplecorn to carry Triplecobs between cob baskets. The manager has initialy N Triplecobs and a line of baskets numbered from 1 to 10000. Each of the Triplecobs is in one of the baskets. The manager wants toby to group the Triplecobs on K baskets or less in a way he minimices the amount of tobycookies he must give to Toby. Toby is a small dog, and can only carry one TripleCob at a time. So Toby must do his job by picking a Corncob on some basket, walking to some other basket and releasing the Triplecob there. The reward for Toby is a tobycookie for every meter that he walks with one of those taisty cobs on his mouth (Without eating it). The distance between two consecutive baskets in one meter. Remember toby doesn’t charge if he walks without a Triplecorn on his mouth. Input The input consists of several test cases. Each test case begins with a line with two integers N and K. Then follows one line with N integers Ci indicating the initial basket of the Triplecob i. • 1 ≤ N ≤ 10000 •1≤K≤N • 1≤Ci ≤10000 Output For each test case, print a single line containing the minimum amount of tobycookies the manager must pay Toby to do the job. Explication: In the first test case the objects are already grouped on 3 baskets, so Toby doesn’t need to do any job. In the second test case, Toby can move the cobs in baskets 1 and 3 to basket 2 with cost 1 each (Total cost 2), note that there is no better solution. Sample Input 33 123 31 123 42 1389 Sample Output 0 2 3