#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long minimizeStoveTime(int N, int K, vector<int>& times) {
// Step 1: Sort the times (if not already sorted)
sort(times.begin(), times.end());
// Step 2: Calculate the gaps between consecutive intervals
vector<int> gaps;
for (int i = 1; i < N; ++i) {
gaps.push_back(times[i] - (times[i-1] + 1)); // gap between intervals
}
// Step 3: Sort the gaps in ascending order
sort(gaps.begin(), gaps.end());
// Step 4: Compute the total time
long long total_time = times.back() - times.front() + 1; // Total range of time covered
if (N > K) {
for (int i = 0; i < N - K; ++i) {
total_time -= gaps[i]; // Subtract the smallest (N - K) gaps
}
}
return total_time;
}
int main() {
// Input parsing
int N, K;
cin >> N >> K;
vector<int> times(N);
for (int i = 0; i < N; ++i) {
cin >> times[i];
}
// Compute and output the result
cout << minimizeStoveTime(N, K, times) << endl;
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... |