#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("arch=skylake,tune=native,sse4")
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
constexpr ll INF = 1e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto& x : a) cin >> x;
sort(a.begin(), a.end());
vector<int> na;
for (int i = 0; i < n; ++i) {
if (i + 1 < n && a[i] == a[i + 1]) {
k--;
i++;
} else na.push_back(a[i]);
}
if (k <= 0) {
cout << 0 << '\n';
return 0;
}
n = (int)na.size();
vector<vector<ll>> dp(3, vector<ll>(k + 1, INF));
dp[1][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= k; ++j) {
dp[2][j] = min(dp[2][j], dp[1][j]);
}
if (i >= 2) {
ll x = na[i - 1] - na[i - 2];
for (int j = 1; j <= k; ++j) {
dp[2][j] = min(dp[2][j], dp[0][j - 1] + x);
}
}
swap(dp[0], dp[1]);
swap(dp[1], dp[2]);
}
cout << min({dp[0][k], dp[1][k], dp[2][k]}) << '\n';
}