#include <bits/stdc++.h>
using namespace std;
int main (){
ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
vector <int> a(n), dif;
for (int &i : a) cin >> i;
for (int i = 1; i < n; i++)
dif.push_back(a[i] - a[i - 1]);
sort(dif.begin(), dif.end());
int sum = 0;
for (int i = 0; i < n - k; i++)
sum += dif[i];
int ans = sum + k;
for (int i = n - k; i < n - 1; i++){
sum += dif[i];
ans = min(ans, dif[i] + n - i);
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |