#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
int k; cin >> k;
vector<int> nums(n);
vector<int> spaces(n-1);
for(int i = 0; i < n; i++) {
cin >> nums[i];
}
for(int i = 0; i < n-1; i++) {
spaces[i] = nums[i+1] - nums[i] - 1;
}
sort(spaces.rbegin(), spaces.rend());
//for(auto i : nums) {
//cout << i << " " ;
//} cout << endl;
//for(auto i : spaces) {
//cout << i << " " ;
//}cout << endl;
int ans = nums[n-1] - nums[0] + 1;
for(int i = 0; i < k-1; i++) {
ans -= spaces[i];
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |