#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int t[N];
signed main() {
cin.tie(0)->sync_with_stdio(false);
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> t[i];
}
sort(t + 1, t + n + 1); // sort nếu chưa đảm bảo
vector<int> gaps;
for(int i = 1; i < n; i++) {
int gap = t[i + 1] - (t[i] + 1);
if(gap > 0) gaps.push_back(gap);
}
// tổng thời gian tối thiểu nếu bật riêng từng khách
int total_time = n;
// cần ghép lại N - K đoạn → chọn N - K khoảng trống nhỏ nhất
sort(gaps.begin(), gaps.end());
for(int i = 0; i < n - k && i < (int)gaps.size(); i++) {
total_time += gaps[i];
}
cout << total_time << '\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... |