//gpt
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
if (!(cin >> N >> K)) return 0;
vector<long long> T(N);
for (int i = 0; i < N; ++i) cin >> T[i];
if (K >= N) {
cout << N << "\n";
return 0;
}
vector<long long> gaps;
for (int i = 0; i + 1 < N; ++i) {
long long g = T[i+1] - T[i] - 1; // >= 0
gaps.push_back(g);
}
sort(gaps.begin(), gaps.end(), greater<long long>()); // giảm dần
long long total_gaps = 0;
for (auto x : gaps) total_gaps += x;
// bỏ K-1 largest gaps
long long reduce = 0;
for (int i = 0; i < K-1; ++i) reduce += gaps[i];
long long ans = N + (total_gaps - reduce);
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |