#include <bits/stdc++.h>
#define fi           first
#define si           second
#define For(i,a,b)   for (int i = (a), _b =(b); i <= _b; ++i)
#define all(v)       v.begin(), v.end()
#define Unique(v)    v.erase(unique(all(v)), v.end())
#define MASK(i)      (1LL << (i))
#define bit(i,n)     (((n)>>(i)) & 1)
#define Vii          vector<pair<int,int>>
#define setpr(x)     cout<<setprecision(x)<<fixed
#define Prior        priority_queue< pair<int,int> , Vii, greater<Pair> >
using namespace std;
const int Mod = 1E9 + 7;
const long long INF  = 4E18;
const int N = 2e5 + 1;
int a[N],n,k;
vector<int> v;
signed main()
{
    cin >> n >> k;
    For(i,1,n) cin >> a[i];
    For(i,2,n) v.push_back(a[i]-a[i-1]-1);
    sort(v.begin(),v.end(),greater<int>());
    int c = 0;
    For(i,0,k-2)
    {
        c += v[i];
    }
    cout << (a[n]-a[1] + 1) - c;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |