#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int MOD = 1e9 + 7;
const int inf = 2e18;
const int N = 1e5 + 7;
int n, k, a[N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
vector<int> vec;
for(int i = 1; i <= n; i++) {
cin >> a[i];
if(i > 1) vec.pb(a[i - 1] - a[i]);
}
sort(all(vec));
int ans = a[n] - a[1] + k;
for(int i = 0; i < k - 1; i++) {
ans += vec[i];
}
cout << ans << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |