#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
int n, k; cin >> n >> k;
int ans = 0, a[n + 1];
a[0] = -1;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
vector<int> dif;
for (int i = 1; i <= n; i++){
if (a[i] - a[i - 1] != 1) {
ans++;
if (i > 1) dif.push_back(a[i] - a[i - 1] - 1);
}
}
int l = n;
sort(dif.rbegin(), dif.rend());
while (ans > k){
ans--;
l += dif.back();
dif.pop_back();
}
cout << l << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |