#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair
#define pb push_back
#define INF (ll)2e18
#define MOD 998244353
void solve(){
ll n, k; cin >> n >> k;
vector<ll> a(n);
for (ll i=0; i<n; i++) cin >> a[i];
sort(a.begin(), a.end());
vector<ll> intr;
for (ll i=1; i<n; i++){
if (a[i]-1!=a[i]){
intr.push_back(a[i]-a[i-1]-1);
}
}
sort(intr.rbegin(), intr.rend());
ll res=a[n-1]-a[0]+1;
for (ll i=0; i<min((ll)intr.size(), k-1); i++){
res-=intr[i];
}
cout << res << ln;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifdef LOCAL
auto start = chrono::high_resolution_clock::now();
#endif
ll t=1;
// cin >> t;
while (t--) solve();
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |