Submission #331351

#TimeUsernameProblemLanguageResultExecution timeMemory
331351limabeansStove (JOI18_stove)C++17
100 / 100
29 ms2416 KiB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;



int n, k;
int a[maxn];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n>>k;
    for (int i=0; i<n; i++) {
	cin>>a[i];
    }

    sort(a,a+n);

    priority_queue<int> gaps;
    for (int i=0; i+1<n; i++) {
	gaps.push(a[i+1]-a[i]-1);
    }

    int res = a[n-1]-a[0]+1;
    for (int i=0; i<k-1; i++) {
	res -= gaps.top();
	gaps.pop();
    }

    cout<<res<<endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...