This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<ll,ll>
#define fi first
#define sec second
const ll MOD = 1e9 + 7;
const ll N = 5e3 + 5;
const ll INF = 1e18;
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int tc = 1;
// cin >> tc;
while(tc--){
ll n,k; cin >> n >> k;
vector<ll>vec(n+5);
for(int i=1;i<=n;i++) cin >> vec[i];
vector<ll>seg;
for(int i=1;i<=n;i++) seg.pb(vec[i] - vec[i-1] - 1);
sort(seg.begin(), seg.end(), greater<ll>());
ll ans = 0;
for(int i=0;i<(k-1);i++) ans += seg[i];
cout << vec[n] - vec[1] + 1 - ans << endl;
}
}
/*
1 2 3 4 5
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |