#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(node >= n){
for(int i = 0; i < MAXN; ++i) dp[node][i] = 1e9;
dp[node][walk[node]] = 0;
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
340 KB |
Output is correct |
3 |
Incorrect |
15 ms |
436 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |