This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
const ll inf = 1e9;
const int mxn = 330;
int N,M;
vector<pii> tree[mxn];
ll dp[mxn][mxn],dep[mxn];
pii par[mxn];
bitset<mxn> boom;
void dfs(int now){
fill(dp[now],dp[now]+mxn,inf);
if(boom[now])dp[now][dep[now]] = 0;
else fill(dp[now],dp[now]+mxn,0);
for(auto [nxt,w]:tree[now]){
//cerr<<now<<":"<<nxt<<endl;
dep[nxt] = dep[now]+w;
dfs(nxt);
for(int i = 0;i<mxn;i++){
dp[now][i] += dp[nxt][i];
}
}
ll tmp[mxn];
fill(tmp,tmp+mxn,inf);
for(int i = 0;i<mxn;i++){
for(int j = 0;j<mxn;j++){
if(i-j>=-par[now].sc)tmp[i] = min(tmp[i],dp[now][j]+abs(i-j));
}
}
//cerr<<now<<' '<<par[now].sc<<":";for(int j = 0;j<=30;j++)cerr<<(tmp[j]>=inf?-1:tmp[j])<<' ';cerr<<endl;
for(int i = 0;i<mxn;i++)dp[now][i] = tmp[i];
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
for(int i = 2;i<=N+M;i++){
int p,c;
cin>>p>>c;
par[i] = pii(p,c);
tree[p].push_back(pii(i,c));
}
for(int i = N+1;i<=N+M;i++)boom[i] = true;
dfs(1);
cout<<*min_element(dp[1],dp[1]+mxn)<<'\n';
return 0;
}
# | 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... |