#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 505;
int n, m, L[MAXN], R[MAXN];
vector<pair<int, int> > adj[MAXN];
int walk[MAXN], dp[MAXN][MAXN];
void precalc(int node){
for(pair<int, int> x : adj[node]){
walk[x.first] = walk[node] + x.second;
precalc(x.first);
}
}
void dfs(int node){
if(adj[node].empty()){
for(int i = 0; i < MAXN; ++i) dp[node][i] = abs(i - walk[node]);
}
for(pair<int, int> x : adj[node]){
dfs(x.first);
for(int i = 0; i < MAXN; ++i){
int mini = 1e9;
for(int j = 0; j < MAXN; ++j){
mini = min(mini, dp[x.first][j] + abs(i - j));
}
dp[node][i] += mini;
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i < n + m; ++i){
int p, c;
cin >> p >> c, --p;
adj[p].push_back({i, c});
}
precalc(0);
dfs(0);
int ans = 1e9;
for(int i = 0; i < MAXN; ++i) ans = min(ans, dp[0][i]);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
396 KB |
Output is correct |
3 |
Incorrect |
15 ms |
524 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |