#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3")
#define Study ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define all(x) x.begin(),x.end()
#define fori(x,y,z) for(ll x=y;x<=z;x++)
const ll INF=1e12;
const ll sz=2e5+10;
const ll mod=1e9+7;
void work(){
ll n,k;
cin>>n>>k;
vector<ll>v(n+10,0);
multiset<ll>st;
fori(i,1,n){
cin>>v[i];
if(i>1){
st.ins(v[i]-v[i-1]-1);
}
}
ll ans=v[n]-v[1]+1;
k--;
while(k--){
ans-=*st.rbegin();
auto it=st.end();
//cout<<k<<" "<<*it<<endl;
it--;
st.erase(it);
if(st.empty() || *st.rbegin()==0)
break;
}
cout<<ans<<endl;
}
int main(){
Study;
ll t=1;
//cin>>t;
fori(i,1,t){
work();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |