Submission #1295060

#TimeUsernameProblemLanguageResultExecution timeMemory
1295060dooweyFireworks (APIO16_fireworks)C++20
19 / 100
17 ms5164 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = 350;
const int C = 1000;
const int M = 2 * C;
const ll inf = (ll)1e18;
vector<pii> T[N];
ll dp[N][M];

void dfs(int u, int w){
    if(T[u].empty()){
        for(int i = 0 ; i < M; i ++ ){
            dp[u][i] = abs(i-C);
        }
        return;
    }
    for(int i = 0 ; i < M; i ++ ){
        dp[u][i]=0;
    }
    for(auto x : T[u]){
        dfs(x.fi, x.se);
        for(int j = 0; j < M; j ++ ){
            if(j < x.se) dp[u][j] = inf;
            else dp[u][j] += dp[x.fi][j-x.se];
            dp[u][j]=min(dp[u][j],inf);
        }
    }
    ll di = inf;
    for(int i = 0 ; i < M; i ++ ){
        di ++ ;
        if(di < dp[u][i]){
            dp[u][i] = di;
        }
        else{
            di = dp[u][i];
        }
    }
    priority_queue<pii, vector<pii>, greater<pii>> boo;
    for(int i = M - 1; i >= 0 ; i -- ){
        boo.push(mp(dp[u][i] + i, i));
        while(boo.top().se > i + w){
            boo.pop();
        }
        dp[u][i] = boo.top().fi - i;
    }
}

int main(){
    fastIO;
    int n, m;
    cin >> n >> m;
    for(int i = 2; i <= n + m ; i ++ ){
        int p, c;
        cin >> p >> c;
        T[p].push_back(mp(i, c));
    }
    dfs(1, 0);
    ll res = inf;
    for(int i = 0 ; i < M; i ++ ){
        res = min(res, dp[1][i]);
    }
    cout << res << "\n";
    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...