#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
const int maxN = 1e5 + 7;
int a[maxN];
void solve() {
/*int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
for(int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
if(m > n / 2) {
cout << -1 << '\n';
return;
}
vector<pair<int, int>> ans;
int pos = n - 1;
for(int i = 0; i < n; i++) {
if(i >= pos) break;
ans.pb({a[pos--].second, a[i].second});
}
cout << ans.size() << '\n';
for(auto [x, y] : ans) {
cout << x + 1 << ' ' << y + 1 << '\n';
}*/
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
int ans = a[n] - a[1];
vector<int> v;
for(int i = 2; i <= n; i++) {
v.pb(a[i] - (a[i - 1] + 1));
}
sort(v.rbegin(), v.rend());
for(int i = 0; i < k - 1; i++) ans -= v[i];
cout << ans + 1 << '\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("input.txt", "r", stdin);
int t = 1;
//cin >> t;
while(t--) 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... |