#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll MOD = 1000000007; int n, k; ll a[100005]; vector > pairs; int main(void){ cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) { if (i == 0){ pairs.push_back({ a[i + 1] - a[i], i }); } else if (i == n - 1){ pairs.push_back({ a[i] - a[i - 1], i }); } else{ pairs.push_back({ a[i + 1] - a[i - 1], i }); } } sort(pairs.begin(), pairs.end()); int cnt = 1; bool one[100005]; for (int i = 0; i < n; i++) one[i] = false; one[-1] = one[n] = true; for (int i = n - 1; i >= 0; i--){ int s = pairs[i].second; if (one[s - 1] || one[s + 1]){ one[s] = true; cnt++; } else if (cnt < k - 1){ one[s] = true; cnt += 2; } if (cnt == k) break; } ll ans = 0; int pos = 0, st; while (pos < n){ st = pos; while (!one[pos]){ pos++; } if (pos == st){ pos++; continue; } ans += a[pos - 1] - a[st]; } cout << ans << endl; }