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 int long long
int DP[301][301];
vector<pair<int,int>> adj[301];
int n,m;
void dfs(int x,int p){
if(x>n){
iota(DP[x],DP[x]+301,0);
return;
}
for(auto&i:adj[x])if(i.first!=p) {
dfs(i.first, x);
for(int j=0;j<=300;j++){
int best = INT64_MAX;
for(int y=0;y<=j;y++)best=min(best,DP[i.first][y]+abs(i.second+y-j));
DP[x][j]+=best;
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i=2;i<=n+m;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(i,b);
adj[i].emplace_back(a,b);
}
dfs(1,0);
cout << *min_element(DP[1],DP[1]+301) << '\n';
}
# | 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... |