#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define nmax 1000007
#define nhanh ios_base::sync_with_stdio(0);cin.tie(NULL),cout.tie(NULL);
#define pll pair<ll,ll>
#define fi first
#define se second
const ll mod = 1e9 + 7;
ll n,k,x,y,a[nmax],dp[2007][2007][3];
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort (a + 1, a + 1 + n, greater<ll>());
memset(dp,0x3f,sizeof(dp));
dp[0][0][0] = 0;
dp[0][0][1] = 0;
for (int j = 1; j <= k; j++)
for (int i = 1; i <= n; i++)
{
dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] - a[i]);
dp[i][j][1] = min(dp[i - 1][j][1], dp[i - 1][j - 1][0] + a[i]);
}
cout <<dp[n][k][0];
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgbm1heCAxMDAwMDA3CiNkZWZpbmUgbmhhbmggaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKE5VTEwpLGNvdXQudGllKE5VTEwpOwojZGVmaW5lIHBsbCBwYWlyPGxsLGxsPgojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgc2Ugc2Vjb25kCmNvbnN0IGxsIG1vZCA9IDFlOSArIDc7CmxsIG4sayx4LHksYVtubWF4XSxkcFsyMDA3XVsyMDA3XVszXTsKaW50IG1haW4oKQp7CiAgIGNpbiA+PiBuID4+IGs7CgogICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgIHsKICAgICAgIGNpbiA+PiBhW2ldOwogICB9CiAgIHNvcnQgKGEgKyAxLCBhICsgMSArIG4sIGdyZWF0ZXI8bGw+KCkpOwogICBtZW1zZXQoZHAsMHgzZixzaXplb2YoZHApKTsKICAgZHBbMF1bMF1bMF0gPSAwOwogICBkcFswXVswXVsxXSA9IDA7CgogICAgIGZvciAoaW50IGogPSAxOyBqIDw9IGs7IGorKykKICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICB7CiAgICAgICBkcFtpXVtqXVswXSA9ICBtaW4oZHBbaSAtIDFdW2pdWzBdLCBkcFtpIC0gMV1bal1bMV0gLSBhW2ldKTsKICAgICAgIGRwW2ldW2pdWzFdID0gIG1pbihkcFtpIC0gMV1bal1bMV0sIGRwW2kgLSAxXVtqIC0gMV1bMF0gKyBhW2ldKTsKICAgfQoKICAgY291dCA8PGRwW25dW2tdWzBdOwp9CgoK