#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll i, j, n, k, a[100001], ans;
bool cmp(ll x, ll y){
return x > y;
}
void run(){
cin >> n >> k;
for( i = 0; i < n; i++) cin >> a[i];
ans = 1+a[n-1]-a[0];
for( i = 0; i < n-1; i++) a[i] = a[i+1]-a[i]-1;
sort(a, a+n-1, cmp);
for( i = 0; i < k-1; i++) ans -= a[i];
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// int tt; cin >> tt; while(tt--)
run();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |