#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pf push_front
const int mod = 1e9 + 7;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int ans = 0;
vector <int> v (n);
vector <int> v2 (n - 1);
for (int i = 0; i < n; i++){
cin >> v[i];
}
sort (v.begin(), v.end());
for (int i = 0; i < n; i++){
v2[i] = v[i + 1] - v[i] + 1;
}
sort (v2.begin(), v2.end());
if (m == 1){
cout << v[n - 1] - v[0] + 1 << endl;
}
else {
for (int i = 0; i < n - m; i++){
ans += v2[i];
}
ans += m - (n - m);
cout << ans << endl;
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |