Submission #306379

#TimeUsernameProblemLanguageResultExecution timeMemory
306379talant117408Stove (JOI18_stove)C++17
100 / 100
28 ms1408 KiB
/*
	Code written by Talant I.D.
*/
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) (v).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 
inline bool isvowel(char ch){
	ch = tolower(ch);
	return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
 
inline bool isprime(int n){
	if(n < 2 || (n%2 == 0 && n != 2)) return false;
	for(int i = 3; i*i <= n; i++)
		if(n%i == 0) return false;
	return true;
}

int main(){
	do_not_disturb
	
	int n, k, i;
    cin >> n >> k;
    k--;
    vector <int> v(n);
    for(auto &to : v) cin >> to;
    sort(all(v));
    int ans = v.back()+1-v[0];
    
    vector <int> times;
    for(i = 1; i < n; i++){
        times.pb(v[i]-v[i-1]-1);
    }
    sort(rall(times));
    for(i = 0; i < k; i++){
        ans -= times[i];
    }
    cout << ans;
    
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...