Submission #1033604

#TimeUsernameProblemLanguageResultExecution timeMemory
1033604cnn008Stove (JOI18_stove)C++17
100 / 100
21 ms2988 KiB
#include "bits/stdc++.h"
using namespace std;

#ifdef N_N_C
#include "debug.h"
#else
#define cebug(...) "Arya"
#endif

#define int long long

const int N=1e5+5;
const int mod=1e9+7;

int n,k,a[N],d[N];
void sol(){
	cin>>n>>k;
	for(int i=1; i<=n; i++) cin>>a[i];
	int ans=k+a[n]-a[1];
	for(int i=1; i<n; i++) d[i]=a[i+1]-a[i];
	int r=k-1;
	sort(d+1,d+n,greater<int>());
	for(int i=1; i<=r; i++) ans-=d[i];
	cout<<ans;
}
signed main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen(".inp", "r", stdin);
    // freopen(".out", "w", stdout);
    int tt=1;
    //cin>>tt; 
    while(tt--){
    	sol();
    }
    cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...