#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e9;
const int md1 = 1e9+7;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) ((int)v.size())
#define forn(i,n) for(int i = 0; i < n; ++i)
#define forbe(i,b,e) for(int i = b; i < e; ++i)
#define pb push_back
#define pry puts("YES")
#define prn puts("NO")
#define endl '\n'
#define fst first
#define scn second
const int N = 1e5;
int p[N], s[N];
int getp(int x){
return p[x] = (p[x] == x) ? x : getp(p[x]);
}
int uni(int a, int b){
a = getp(a);
b = getp(b);
if (a == b) return 0;
if (s[a] > s[b]) swap(a,b);
p[a] = b;
s[b] += s[a];
return 1;
}
using ti = array<int,3>;
void solve(){
int n, k;
cin >> n >> k;
for(int i = 0; i < n; ++i){
p[i] = i;
s[i] = 1;
}
int x, y;
cin >> x;
priority_queue<ti, vector<ti>, greater<ti>> q;
for(int i = 0; i < n-1; ++i){
cin >> y;
q.push({y-x,i,i+1});
x = y;
}
int ans = 0;
while(q.size() && n > k){
auto u = q.top();
q.pop();
if (uni(u[2],u[1])){
ans += u[0];
--n;
}
}
cout << ans + k << endl;
}
int32_t main(){
#ifndef ONPC
ios_base::sync_with_stdio(0);cin.tie(0);
#else
freopen("in", "r", stdin);
#endif
int t = 1;
/* cin >> t; */
while(t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
33 ms |
3664 KB |
Output is correct |
17 |
Correct |
33 ms |
3600 KB |
Output is correct |
18 |
Correct |
35 ms |
3552 KB |
Output is correct |
19 |
Correct |
33 ms |
3656 KB |
Output is correct |
20 |
Correct |
26 ms |
3660 KB |
Output is correct |
21 |
Correct |
19 ms |
3668 KB |
Output is correct |
22 |
Correct |
23 ms |
3660 KB |
Output is correct |
23 |
Correct |
22 ms |
3652 KB |
Output is correct |
24 |
Correct |
16 ms |
3648 KB |
Output is correct |