Submission #738895

#TimeUsernameProblemLanguageResultExecution timeMemory
738895AlphaMale06Stove (JOI18_stove)C++17
100 / 100
22 ms2352 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()


void solve(){
	int n, k;
	cin >> n >> k;
	int a[n];
	for(int i=0; i< n; i++){
		cin >> a[i];
	}	
	sort(a, a+n);
	int ans=a[n-1]-a[0]+1;
	vector<int> rz;
	for(int i=1; i<n; i++){
		rz.pb(a[i]-a[i-1]-1);
	}
	sort(rz.begin(), rz.end());
	k--;
	int p=rz.size()-1;
	while(k--){
		ans-=rz[p];
		p--;
	}
	cout << ans << '\n';
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...