#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll MOD = 1000000007; struct Node { int idx; ll diff; Node(int idx = -1, ll diff = -1) { this->idx = idx; this->diff = diff; } bool operator>(const Node &n) const { return diff < n.diff; } }; int main() { int N, K; cin >> N >> K; vector A; ll a; for (int i = 0; i < N; ++i) { cin >> a; A.push_back(a); } sort(A.begin(), A.end()); priority_queue , greater> pque; for (int i = 0; i + 1 < N; ++i) { ll a = A[i]; ll b = A[i + 1]; pque.push(Node(i, b - a)); } bool dividePoint[N]; memset(dividePoint, false, sizeof(dividePoint)); for (int i = 0; i < K - 1; ++i) { Node node = pque.top(); pque.pop(); // fprintf(stderr, "%d - %d: %lld\n", node.idx, node.idx + 1, node.diff); dividePoint[node.idx] = true; } ll ans = 0; int s = 0; for (int i = 0; i < N; ++i) { if (!dividePoint[i]) continue; // fprintf(stderr, "%d -> %d\n", s, i); ans += A[i] - A[s]; s = i + 1; } ans += A[N - 1] - A[s]; cout << ans << endl; return 0; }