#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 2e5 + 5;
int a[N];
void solve()
{
int n, k;
cin >> n >> k;
k--;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
vector<int> intervals;
sort(a, a + n);
for (int i = 0; i < n - 1; i++)
{
intervals.push_back(a[i + 1] - (a[i] + 1));
}
sort(intervals.begin(), intervals.end(), [&](int a, int b)
{ return a < b; });
int ans = a[n - 1] + 1 - a[0];
while (k and intervals.size())
{
k--;
ans -= intervals.back();
intervals.pop_back();
}
cout << ans << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++)
{
// cout << "Case #" << i << ':' << ' ';
solve();
cout << 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... |