제출 #1356244

#제출 시각아이디문제언어결과실행 시간메모리
1356244dreamofsecretuniverseStove (JOI18_stove)C++20
100 / 100
15 ms4348 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define rep_(i, a, b) for(ll i = a; i > b; i--)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
constexpr ll mod = 1e9+7, inf = LLONG_MAX, N = 2e5+23;
ll n, k;
ll t[N], p[N], sz[N];
pll a[N];
ll find(ll u){
    if(u==p[u]) return u;
    return p[u] = find(p[u]);
}
bool join(ll u, ll v){
    u = find(u), v = find(v);
    if(u==v) return false;
    if(sz[u]<sz[v]) swap(u, v);
    sz[u] += sz[v];
    p[v] = u;
    return true;
}
void solve(){
    cin >> n >> k;
    for(ll i = 0; i < n; ++i) p[i] = i;
    for(ll i = 0; i < n; ++i) cin >> t[i];
    for(ll i = 1; i < n; ++i) a[i-1] = {t[i]-t[i-1], i-1};
    sort(a, a+(n-1));
    ll cnt = n, res = k;
    for(ll i = 0; i < n-1; ++i){
        if(cnt==k) break;
        // cout << a[i].first << " " << a[i].second;
        bool cur = join(a[i].second, a[i].second+1);
        res += a[i].first;
        if(cur) --cnt;
    }
    cout << res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...