#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 |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
6 ms |
604 KB |
Output is correct |
6 |
Correct |
6 ms |
604 KB |
Output is correct |
7 |
Correct |
7 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
928 KB |
Output is correct |
10 |
Correct |
10 ms |
856 KB |
Output is correct |
11 |
Correct |
11 ms |
868 KB |
Output is correct |
12 |
Correct |
12 ms |
860 KB |
Output is correct |
13 |
Correct |
13 ms |
1116 KB |
Output is correct |
14 |
Correct |
14 ms |
1112 KB |
Output is correct |
15 |
Correct |
14 ms |
1116 KB |
Output is correct |
16 |
Correct |
14 ms |
1048 KB |
Output is correct |
17 |
Correct |
14 ms |
1116 KB |
Output is correct |
18 |
Correct |
14 ms |
1188 KB |
Output is correct |
19 |
Correct |
14 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |