This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int solve(int K, vector<int> & arrivals) {
int N = (int) arrivals.size();
vector<int> gapSizes(N-1);
for (int i = 0; i < N-1; i++)
gapSizes[i] = arrivals[i+1]-arrivals[i]-1;
sort(gapSizes.begin(), gapSizes.end(), greater<int>());
int answer = arrivals[N-1]+1 - arrivals[0];
for (int i = 0; i < K-1; i++) {
answer -= gapSizes[i];
}
return answer;
}
int main() {
int N, K;
vector<int> arrivals;
cin >> N >> K;
arrivals = vector<int>(N);
for (int i = 0; i < N; i++)
cin >> arrivals[i];
cout << solve(K, arrivals) << '\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... |