#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define REP(i,r) for(ll i=0,_r=(r);i<_r;i++)
#define FOR(i,l,r) for(ll i=(l),_r=(r);i<=_r;i++)
#define FORE(i,v) for(__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
#define FORNG(i,r,l) for(ll i=(r),_l=(l);i>=_l;i--)
#define MASK(i) (1LL<<(i))
#define BIT(x,i) (((x)>>(i))&1LL)
#define all(v) (v).begin(),(v).end()
#define size(v) ((ll)(v).size())
const ll MOD = 1e9+7, N = 2e5+10, INF = 1e18, LOG = 20;
int main(){
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(0);
ll n,k;cin>>n>>k;
k--;
vector<ll> a(n + 1, 0),gap;
FOR(i,1,n)cin>>a[i];
FOR(i,2,n)gap.pb(a[i] - a[i - 1] - 1);
ll ans = a[n] - a[1] + 1;
sort(all(gap), greater<>());
REP(i,k)ans -= gap[i];
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |