#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Waimai ios::sync_with_stdio(false),cin.tie(0)
#define FOR(x,a,b) for(int x=a;x<=b;x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 3e5 + 5;
struct Seg {int pos, l, r;};
int n, k, penalty;
int a[SIZE], pre[SIZE];
pair<int, int> dp[SIZE];
deque<Seg> st;
int dis (int l, int r) {
int mid = (l + r) / 2, ldis, rdis;
ldis = (mid - l + 1) * a[mid] - (pre[mid] - pre[l - 1]);
rdis = (pre[r] - pre[mid - 1]) - (r - mid + 1) * a[mid];
return ldis + rdis;
}
pair<int, int> cal (int l, int r) {
return {dp[l].F + dis (l + 1, r) + penalty, dp[l].S + 1};
}
bool ok (int p) {
penalty = p;
st.clear();
st.push_back ({0, 1, n});
FOR (i, 1, n) {
while (st.size() && st[0].r < i) {
st.pop_front();
}
dp[i] = cal (st[0].pos, i);
while (st.size() && cal (i, st.back().l) <= cal (st.back().pos, st.back().l)) {
st.pop_back();
}
if (!st.size()) {
st.push_back ({i, i + 1, n});
} else if (cal (i, st.back().r) >= cal (st.back().pos, st.back().r)) {
if (st.back().r != n) {
st.push_back ({i, st.back().r + 1, n});
}
} else {
auto [pos, l, r] = st.back();
while (l < r) {
int mid = (l + r) / 2;
if (cal (i, mid) > cal (pos, mid)) {
l = mid + 1;
} else {
r = mid;
}
}
st[st.size() - 1].r = l - 1;
st.push_back ({i, l, n});
}
}
return dp[n].S <= k;
}
void solve() {
cin >> n >> k;
FOR (i, 1, n) cin >> a[i];
sort (a + 1, a + n + 1);
FOR (i, 1, n) pre[i] = pre[i - 1] + a[i];
int l = 0, r = 5e10;
while (l < r) {
int mid = (l + r) / 2;
if (ok (mid)) {
r = mid;
} else {
l = mid + 1;
}
}
ok (l);
cout << dp[n].F - k * penalty << '\n';
}
int32_t main() {
Waimai;
solve();
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk8zIikKI2luY2x1ZGUgPGJpdHMvc3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nQoKTsKICAgICAgICB9CiAgICAgICAgZHBbaV0gPSBjYWwgKHN0WzBdLnBvcywgaSk7CiAgICAgICAgd2hpbGUgKHN0LnNpemUoKSAmJiBjYWwgKGksIHN0LmJhY2soKS5sKSA8PSBjYWwgKHN0LmJhY2soKS5wb3MsIHN0LmJhY2soKS5sKSkgewogICAgICAgICAgICBzdC5wb3BfYmFjaygpOwogICAgICAgIH0KICAgICAgICBpZiAoIXN0LnNpemUoKSkgewogICAgICAgICAgICBzdC5wdXNoX2JhY2sgKHtpLCBpICsgMSwgbn0pOwogICAgICAgIH0gZWxzZSBpZiAoY2FsIChpLCBzdC5iYWNrKCkucikgPj0gY2FsIChzdC5iYWNrKCkucG9zLCBzdC5iYWNrKCkucikpIHsKICAgICAgICAgICAgaWYgKHN0LmJhY2soKS5yICE9IG4pIHsKICAgICAgICAgICAgICAgIHN0LnB1c2hfYmFjayAoe2ksIHN0LmJhY2soKS5yICsgMSwgbn0pOwogICAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYXV0byBbcG9zLCBsLCByXSA9IHN0LmJhY2soKTsKICAgICAgICAgICAgd2hpbGUgKGwgPCByKSB7CiAgICAgICAgICAgICAgICBpbnQgbWlkID0gKGwgKyByKSAvIDI7CiAgICAgICAgICAgICAgICBpZiAoY2FsIChpLCBtaWQpID4gY2FsIChwb3MsIG1pZCkpIHsKICAgICAgICAgICAgICAgICAgICBsID0gbWlkICsgMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgciA9IG1pZDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBzdFtzdC5zaXplKCkgLSAxXS5yID0gbCAtIDE7CiAgICAgICAgICAgIHN0LnB1c2hfYmFjayAoe2ksIGwsIG59KTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZHBbbl0uUyA8PSBrOwp9Cgp2b2lkIHNvbHZlKCkgewogICAgY2luID4+IG4gPj4gazsKICAgIEZPUiAoaSwgMSwgbikgY2luID4+IGFbaV07CiAgICBzb3J0IChhICsgMSwgYSArIG4gKyAxKTsKICAgIEZPUiAoaSwgMSwgbikgcHJlW2ldID0gcHJlW2kgLSAxXSArIGFbaV07CiAgICBpbnQgbCA9IDAsIHIgPSA1ZTEwOwogICAgd2hpbGUgKGwgPCByKSB7CiAgICAgICAgaW50IG1pZCA9IChsICsgcikgLyAyOwogICAgICAgIGlmIChvayAobWlkKSkgewogICAgICAgICAgICByID0gbWlkOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGwgPSBtaWQgKyAxOwogICAgICAgIH0KICAgIH0KICAgIG9rIChsKTsKICAgIGNvdXQgPDwgZHBbbl0uRiAtIGsgKiBwZW5hbHR5IDw8ICdcbic7Cn0KCmludDMyX3QgbWFpbigpIHsKICAgIFdhaW1haTsKICAgIHNvbHZlKCk7Cn0=