| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1367160 | edo | Stove (JOI18_stove) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct DSU {
int n, cnt;
//ll cost;
vector<int> par, sz;
//map<int, ll> costG;
DSU(int n) : n(n), par(n), sz(n, 1) {
iota(par.begin(), par.end(), 0);
//cost = 0;
cnt = 0;
}
//pair<int, ll> leader(int x) {
int leader(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
int merge(int a, int b) {
a = leader(a), b = leader(b);
if(a == b) return 0;
if(sz[a] < sz[b])
swap(a, b);
sz[a] += sz[b];
costG[a] += 1;
par[b] = a;
return 1;
}
//ll get_cost() {
//ll res = 0;
//for(auto [x, y] : costG) {
//res += y;
//}
//return res;
//}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll k;
cin >> n >> k;
vector<ll> a(n);
for(ll &x : a)
cin >> x;
if(n == k) {
cout << n << "\n";
return 0;
}
ranges::sort(a);
//const ll inf = (1ll << 60);
DSU dsu(n);
vector<ll> gap;
int m = 1;
for(int i = 1; i < n; ++i) {
if(a[i] == a[i - 1] + 1)
continue;
m++;
gap.push_back(a[i] - a[i - 1] - 1);
}
if(m <= k) {
cout << n << "\n";
return 0;
}
ranges::sort(gap);
ll ans = n;
int need = m - k;
for(int i = 0; i < need; ++i) {
ans += gap[i];
}
cout << ans << "\n";
return 0;
}
//et koliko sam vremena potrosio
