#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define Mor_Te ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define intt long long
#define ll long long
#define pb push_back
#define all(v) v.begin() , v.end()
#define rall(v) v.rbegin() , v.rend()
#define F first
#define S second
#define szz(x) ((int)(x).size())
#define pii pair<intt , intt>
using namespace std;
using namespace __gnu_pbds;
const intt sz = 1e6 + 5;
const intt INF = 1e18;
const intt MOD = 998244353;
intt C(intt n , intt k){
if(n == 1 and k > 1){
return 0;
}
intt ans = 1;
if(k > n - k){
k = n - k;
}
for(intt i = 1; i <= k; i++){
intt g = gcd(ans , i);
ans = ans / g * ((n - k + i) / (i / g));
}
return ans;
}
bool isprime(int n){
if(n < 2) return false;
if(n % 2 == 0) return n == 2;
if(n % 3 == 0) return n == 3;
for(int i = 5; i * i <= n; i += 6){
if(n % i == 0 || n % (i+2) == 0) return false;
}
return true;
}
intt count_divisors(int n){
intt cnt = 0;
for(intt i = 1; i * i <= n; i++){
if(n % i == 0){
cnt++;
if(i != n / i) cnt++;
}
}
return cnt;
}
void Yusiff(){
int n , k; cin >> n >> k;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
vector<intt> v;
if(n <= k){
cout << n << '\n'; return;
} else{
for(int i = 1; i < n; i++){
v.pb(a[i] - a[i - 1]);
}
sort(all(v));
int m = n - k;
intt ans = 0;
for(int i = 0; i < m; i++){
ans += v[i];
}
cout << n + ans - (n - k) << '\n'; return;
}
}
signed main(){
Mor_Te
int t = 1;
//cin >> t;
for(intt i = 1; i <= t; i++){
Mor_Te
Yusiff();
}
}
/* BY Yusif Nazarli */
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |