#include <bits/stdc++.h>
using namespace std;
long long dp[3002][3002] ;
int N , K ;
const long long INF = 1e18 + 100 ;
long long A[3003] ;
long long solve(int pos,int present){
if(pos == N + 1){
if(present == K)
return 0 ;
return INF ;
}
long long &ret = dp[pos][present] ;
if(ret != -1)
return ret ;
long long sizeA = present ; // current sizeof set A
long long sizeB = pos - 1 - sizeA ; // current sizeof set B
// put in set B
ret = A[pos]*present - (K - sizeA)*A[pos] + solve(pos + 1,present) ;
// put in set A
if(present < K)
ret = min(ret , sizeB*A[pos] - (N - K - sizeB)*A[pos] + solve(pos + 1,present + 1)) ;
return ret ;
}
int main(int argc,char* argv[]) {
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin) ;
// freopen("out.txt", "w", stdout);
#endif
cin >> N >> K ;
for(int i = 1 ; i <= N ; i++)
cin >> A[i] ;
sort(A + 1 , A + N + 1) ;
memset(dp , -1 , sizeof(dp)) ;
cout << solve(1 , 0) << endl ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpsb25nIGxvbmcgZHBbMzAwMl1bMzAwMl0gOwppbnQgTiAsIEsgOwpjb25zdCBsb25nIGxvbmcgSU5GID0gMWUxOCArIDEwMCA7CmxvbmcgbG9uZyBBWzMwMDNdIDsKbG9uZyBsb25nIHNvbHZlKGludCBwb3MsaW50IHByZXNlbnQpewogICAgaWYocG9zID09IE4gKyAxKXsKICAgICAgICBpZihwcmVzZW50ID09IEspCiAgICAgICAgICAgIHJldHVybiAwIDsKICAgICAgICByZXR1cm4gSU5GIDsKICAgIH0KICAgIGxvbmcgbG9uZyAmcmV0ID0gZHBbcG9zXVtwcmVzZW50XSA7CiAgICBpZihyZXQgIT0gLTEpCiAgICAgICAgcmV0dXJuIHJldCA7CiAgICBsb25nIGxvbmcgc2l6ZUEgPSBwcmVzZW50IDsgLy8gY3VycmVudCBzaXplb2Ygc2V0IEEKICAgIGxvbmcgbG9uZyB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