This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
#define el cout << '\n';
using namespace std;
const int N = 1e5+5;
const int inf = 1e18;
const int LG = 31;
//const int mod = 1e9+7;
const int mod = 998244353;
int n,a[N],k;
void solve(){
cin>>n>>k;
for(int i=1; i<=n; i++ ) cin>>a[i];
vector<int> v;
for(int i=1; i<n; i++) v.pb(a[i+1]-a[i]);
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
if(k>=n) {
cout << n;
return;
}
if(k==1){
cout << a[n]-a[1]+1;
return;
}
int ans=0;
for(int i=k-1; i<v.size(); i++) ans+=v[i];
cout << ans+k;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
el;
}
}
// 1 2 5 4 3 1 3 2 4 5
Compilation message (stderr)
stove.cpp: In function 'void solve()':
stove.cpp:35:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=k-1; i<v.size(); i++) ans+=v[i];
| ~^~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |