//bekarys
#include <bits/stdc++.h>
using namespace std;
//////
/////
#define sped ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define YES {cout << "YES";return;}
#define Yes {cout << "Yes";return;}
#define No {cout << "No";return;}
#define NO {cout << "NO";return;}
#define check {cout << "CHECK";}
#define s second
#define f first
#define int long long
/*
int bl = 450;
struct query {
long long l, r, ind;
};
query q[200005];
bool cmp(query a,query b){
if(a.l/bl!=b.l/bl)
return a.l<b.l;
return a.r<b.r;
}
*/
const int mod = 1e9+7;
long long bp(long long a , long long b){
if(b == 0) return 1;
if(b % 2 == 1) return (a * bp(a, b - 1)) % mod;
long long p = bp(a, b / 2);
return (p * p) % mod;
}
const int N = 2e5 + 7;
void solve(){
int n;
cin >> n;
int k; cin >> k;
int a[n + 1];
vector < int > v;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
sort(a + 1,a + 1 + n);
int ans = 0;
for(int i = 1;i < n;i++){
v.pb(a[i + 1] - a[i]);
ans += a[i + 1] - a[i];
}
sort(v.rbegin(),v.rend());
for(int i = 0;i < k - 1;i++){
ans -= v[i];
}
ans += k;
cout << ans;
}
signed main(){
int t=1;
// cin >> t;
while(t--){
solve();
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |