#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
#define int long long
#define pb push_back
signed main() {
int n,k;
cin >>n >> k;
k --;
vector<int>a(n + 1);
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
int ans = a[n] - a[1] + 1;
vector<int>b(n);
for(int i = 1; i < n; i ++){
b[i] = a[i + 1] - a[i] - 1;
}
sort(b.begin() + 1,b.end());
reverse(b.begin() + 1,b.end());
for(int i = 1; i <= k; i ++){
ans -= b[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... |