#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n,k,ans,all;
int a[N],pa[N];
priority_queue<pair<int,pair<int,int> > > q;
int root(int x)
{
if(pa[x]==x) return x;
else return pa[x] = root(pa[x]);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for(int i = 0;i < n;i++) cin >> a[i],pa[i] = i;
sort(a,a+n);
for(int i = 0;i < n-1;i++) q.push({a[i]-a[i+1],{i,i+1}}),all+=a[i+1]-a[i];
for(int i = 0;i < n-k;i++)
{
while(!q.empty() and root(q.top().second.first)==root(q.top().second.second)) q.pop();
int a = q.top().second.first,b = q.top().second.second;
ans-=q.top().first;
pa[root(a)] = root(b);
}
cout << ans+k;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
432 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
432 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
37 ms |
3832 KB |
Output is correct |
17 |
Correct |
38 ms |
3740 KB |
Output is correct |
18 |
Correct |
36 ms |
3832 KB |
Output is correct |
19 |
Correct |
35 ms |
3832 KB |
Output is correct |
20 |
Correct |
29 ms |
3804 KB |
Output is correct |
21 |
Correct |
21 ms |
3804 KB |
Output is correct |
22 |
Correct |
20 ms |
3824 KB |
Output is correct |
23 |
Correct |
19 ms |
3832 KB |
Output is correct |
24 |
Correct |
21 ms |
3832 KB |
Output is correct |