#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define all(a) a.begin(), a.end()
const int MOD = 1e9 + 7,INF = 1e18;
void solve(){
int n , k;
cin >> n >> k;
vector <int> a(n);
for(int i = 0;i < n;i++){
cin >> a[i];
}
int ans = n;
vector <int> b;
for(int i = 1;i < n;i++){
b.pb(a[i] - (a[i - 1] + 1));
}
sort(all(b));
int h = n - k;
for(int i = 0;i < h;i++){
ans += b[i];
}
cout<<ans<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int ti = 1;
// cin >> ti;
while (ti--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |