#include<bits/stdc++.h>
#define ll long long
const int nmax = 2e5 + 5, N = 4e5;
const ll oo = 1e9;
const int lg = 18, M = 4e3;
const int base = 2e5, mod = 1e9 + 7;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
int n, k, a[nmax];
struct node{
int u, v, w;
};
int r[nmax];
int get(int u){
return r[u] ? r[u] = get(r[u]) : u;
}
ll sz[nmax];
bool vis[nmax];
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >>k;
for(int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
vector<node> tmp;
for(int i = 2; i <= n; ++i){
tmp.push_back({i, i - 1, max(0, a[i] - a[i - 1] - 1)});
}
sort(tmp.begin(), tmp.end(), [](node a, node b){
return a.w < b.w;
});
for(int i = 1; i <= n; ++i) sz[i] = 1;
for(int i = 0; i < n - k; ++i){
int x = get(tmp[i].u);
int y = get(tmp[i].v);
if(x != y){
r[x] = y;
sz[y] += sz[x] + tmp[i].w;
}
}
ll ans = 0;
for(int i = 1; i <= n; ++i){
int x = get(i);
if(vis[x]) continue;
vis[x] = 1;
ans += sz[x];
}
cout << ans;
}
/*
1_t1 + 1
*/
Compilation message
stove.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
17 ms |
5068 KB |
Output is correct |
17 |
Correct |
19 ms |
5072 KB |
Output is correct |
18 |
Correct |
21 ms |
5036 KB |
Output is correct |
19 |
Correct |
17 ms |
5072 KB |
Output is correct |
20 |
Correct |
20 ms |
5072 KB |
Output is correct |
21 |
Correct |
16 ms |
5072 KB |
Output is correct |
22 |
Correct |
16 ms |
5056 KB |
Output is correct |
23 |
Correct |
16 ms |
5068 KB |
Output is correct |
24 |
Correct |
19 ms |
5060 KB |
Output is correct |