# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1280709 | StefanSebez | Fireworks (APIO16_fireworks) | C++20 | 118 ms | 3920 KiB |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
const int N=5050;
const ll inf=1e18;
void chmn(ll &x,ll y){x=min(x,y);}
void chmx(ll &x,ll y){x=max(x,y);}
vector<pair<int,int>>E[N];
ll Val[N];
int n,m;
void DFS1(int u){
for(auto [i,w]:E[u]){
Val[i]=Val[u]+w;
DFS1(i);
}
}
ll dp[N][N];
void DFS(int u){
if(E[u].empty()){
for(int i=1;i<=n;i++) dp[u][i]=inf;
dp[u][u]=0;
return;
}
for(auto [i,w]:E[u]) DFS(i);
for(int j=1;j<=n;j++){
dp[u][j]=0;
for(auto [i,w]:E[u]){
ll mn=inf;
for(int k=1;k<=n;k++){
if(Val[k]-w<=Val[j]) chmn(mn,dp[i][k]+abs(Val[j]-Val[k]));
}
dp[u][j]+=mn;
chmn(dp[u][j],inf);
}
}
}
int main(){
scanf("%i%i",&n,&m);
n+=m;
for(int i=2;i<=n;i++){
int p,w;scanf("%i%i",&p,&w);
E[p].pb({i,w});
}
DFS1(1);
DFS(1);
ll res=inf;
for(int i=1;i<=n;i++) chmn(res,dp[1][i]);
printf("%lld\n",res);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |