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 <bits/stdc++.h>
#define ll long long
#define oo 1e9
#define pii pair<int, int>
using namespace std;
const int MAX = 1e5 + 5;
int n, k;
int arr[MAX];
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr, arr + n);
vector<int> ranges;
for (int i = 0; i < n - 1; i++)
{
ranges.emplace_back(arr[i + 1] - arr[i] - 1);
}
int ans = arr[n - 1] - arr[0] + 1;
sort(ranges.begin(), ranges.end());
reverse(ranges.begin(), ranges.end());
for (int i = 0; i < k - 1; i++)
{
ans -= ranges[i];
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |