#include <bits/stdc++.h>
#define f first
#define s second
#define ii pair<int, int>
#define iii pair<ii, int>
#define pb push_back
#define save_time ios_base::sync_with_stdio(false); cin.tie(NULL);
const int inf = 1e9 + 7;
using namespace std;
bool comp(int a, int b) {
return a > b;
}
int main() {
save_time;
int n, k;
cin >> n >> k;
vector<long long> v(n);
for (auto &i : v) {
cin >> i;
}
vector<long long> time(n - 1);
for (int i = 0; i < n - 1; i++) {
time[i] = v[i + 1] - v[i] - 1;
}
sort(time.begin(), time.end(), comp);
long long ans = v[n - 1] - v[0] + 1;
for (int i = 0; i < k - 1; i++) {
ans -= time[i];
}
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |