//AzaLE (Azamat Alisherov)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast,unroll-loops")
#define size(x) (int)x.size()
#define int long long
//#define endl '\n'
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F first
#define S second
template<class T> using oset = tree<T, null_type, less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update>;
const int MOD = 1e9 + 7;// 119 * (1 << 23) + 1;
int binpow (int a, int n){
int res = 1;
while (n){
if (n & 1){
res = (res * a) % MOD;
}
a = (a * a) % MOD;
n >>= 1;
}
return res % MOD;
}
void chomp(){
int n, k;
cin >> n >> k;
vector <int> v(n);
for(int i = 0; i < n; i++){
cin >> v[i];
}
vector <int> diff(n - 1);
for(int i = 1; i < n; i++){
diff[i - 1] = abs(v[i] - v[i - 1]);
}
sort(rall(diff));
k--;
int ans = v[n - 1] - v[0] + 1;
for(int i = 0; i < k; i++){
ans -= diff[i] - 1;
}
cout << ans;
}
signed main(){
//ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int tasks = 1;
//cin >> tasks;
while(tasks--){
chomp();
}
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |