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 int mxn = 330;
int N,M;
vector<pii> tree[mxn];
int dp[mxn][mxn],dep[mxn];
int par[mxn];
bitset<mxn> boom;
void dfs(int now){
if(boom[now]){
for(int i = 0;i<mxn;i++)dp[now][i] = abs(dep[now]-i);
}
for(auto [nxt,w]:tree[now]){
dep[nxt] = dep[now]+w;
dfs(nxt);
for(int i = 0;i<mxn;i++){
int mn = 1e9;
for(int j = 0;j<mxn;j++){
mn = min(mn,dp[nxt][j]+abs(i-j));
}
dp[now][i] += mn;
}
}
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] = p;
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... |