제출 #1337418

#제출 시각아이디문제언어결과실행 시간메모리
1337418baodatFireworks (APIO16_fireworks)C++20
100 / 100
173 ms70784 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define FOR(i, l, r) for(int i = l; i <= r; i++)
#define FORD(i, l, r) for(int i = l; i >= r; i--)
#define db double
#define ldb long double
#define all_1(x) (x).begin() + 1, (x).end()
#define all(x) (x).begin(), (x).end()
#define ins insert
#define pb push_back

template<typename T>void debug_var(const T& var, const string& name){
    cerr << name << ": " << var << "\n";
}
template<typename T>void debug_1d(const T& vt, const string& name){
    if(vt.empty()){
        cerr << name << " is empty!\n";
        return;
    }
    FOR(i, 0, (int)vt.size() - 1){
        cerr << name << "[" << i << "]: " << vt[i] << "\n";
    }
}

const int N = 3e5 + 5;
const ll oo = 2e18;
int n, orig_n;
ll w[N];
vector<pair<int, ll>> adj[N];

namespace sub2{
    ll dp[305][305];
    void dfs(int u, int p){
        bool is_leaf = true;
        for(auto [v, w_edge] : adj[u]){
            if(v == p)continue;
            is_leaf = false;
            dfs(v, u);
        }
        if(is_leaf){
            dp[u][0] = 0;
            FOR(i, 1, 300) dp[u][i] = oo;
            return;
        }
        FOR(i, 1, 300) dp[u][i] = 0;
        for(pair<int, ll> e : adj[u]){
            int v = e.first;
            ll w_edge = e.second;
            if(v == p) continue;

            FOR(x, 0, 300){
                ll min_cost = oo;
                FOR(y, 0, x){
                    if(dp[v][y] != oo){
                        min_cost = min(min_cost, dp[v][y] + llabs(x - y - w_edge));
                    }
                }
                dp[u][x] += min_cost;
            }
        }
    }
    void sol(){
        dfs(1, -1);
        ll ans = oo;
        FOR(i, 0, 300) ans = min(ans, dp[1][i]);
        cout << ans << "\n"; 
    }
}

namespace sub4{
    priority_queue<ll> pq[N];
    ll total_w = 0;

    void dfs(int u, int p) {
        if (u > orig_n) {
            pq[u].push(w[u]);
            pq[u].push(w[u]);
            return;
        }
        int child_count = 0;
        for (auto e : adj[u]) {
            int v = e.first;
            if (v == p) continue;
            child_count++;
            dfs(v, u);
            if (pq[u].size() < pq[v].size()) {
                swap(pq[u], pq[v]);
            }
            while (!pq[v].empty()) {
                pq[u].push(pq[v].top());
                pq[v].pop();
            }
        }
        FOR(i, 1, child_count - 1) {
            pq[u].pop();
        }
        ll R = pq[u].top(); pq[u].pop();
        ll L = pq[u].top(); pq[u].pop();
        pq[u].push(L + w[u]);
        pq[u].push(R + w[u]);
    }

    void sol() {
        total_w = 0;
        FOR(i, 2, n) {
            total_w += w[i];
        }
        dfs(1, -1);
        pq[1].pop();
        ll ans = total_w;
        while (!pq[1].empty()) {
            ans -= pq[1].top();
            pq[1].pop();
        }
        cout << ans << "\n";
    }
}

void solve(){
    int m;
    cin >> orig_n >> m;
    n = orig_n + m;
    FOR(i, 2, n){
        int p;
        ll c;
        cin >> p >> c;
        adj[p].pb({i, c});
        adj[i].pb({p, c});
        w[i] = c;
    }
    sub4::sol();
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while(t--){
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...