#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define rep(x,y,z) for (int x=y;x<z;x++)
bool cmp(int a, int b) {
return a > b;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
--k;
int arr[n];
for (int i=0; i<n; ++i) cin >> arr[i];
int ans = arr[n-1]-arr[0]+1;
vector<int> diff(n-1);
for (int i=0; i<n-1; ++i)
diff[i] = arr[i+1]-arr[i]-1;
sort(all(diff), cmp);
for (int i=0; i<k; ++i)
ans -= diff[i];
cout << ans;
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... |