#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e14;
const int MAXN = 505;
int n, m, L[MAXN], R[MAXN];
vector<pair<int, int> > adj[MAXN];
ll 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 < n + m; ++i) dp[node][i] = abs(walk[i] - walk[node]);
}
for(pair<int, int> x : adj[node]){
dfs(x.first);
for(int i = 0; i < n + m; ++i){
ll mini = inf;
for(int j = 0; j < n + m; ++j){
if(x.second + walk[i] - walk[j] >= 0) mini = min(mini, dp[x.first][j] + abs(walk[i] - walk[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);
ll ans = inf;
for(int i = 0; i < n + m; ++i) ans = min(ans, dp[0][i]);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
808 KB |
Output is correct |
6 |
Correct |
3 ms |
1112 KB |
Output is correct |
7 |
Correct |
5 ms |
856 KB |
Output is correct |
8 |
Correct |
8 ms |
1116 KB |
Output is correct |
9 |
Correct |
9 ms |
1116 KB |
Output is correct |
10 |
Correct |
12 ms |
1308 KB |
Output is correct |
11 |
Correct |
16 ms |
1368 KB |
Output is correct |
12 |
Correct |
21 ms |
1352 KB |
Output is correct |
13 |
Correct |
25 ms |
1448 KB |
Output is correct |
14 |
Correct |
26 ms |
1620 KB |
Output is correct |
15 |
Correct |
33 ms |
1620 KB |
Output is correct |
16 |
Correct |
32 ms |
1620 KB |
Output is correct |
17 |
Correct |
32 ms |
1628 KB |
Output is correct |
18 |
Correct |
33 ms |
1624 KB |
Output is correct |
19 |
Correct |
32 ms |
1616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
808 KB |
Output is correct |
16 |
Correct |
3 ms |
1112 KB |
Output is correct |
17 |
Correct |
5 ms |
856 KB |
Output is correct |
18 |
Correct |
8 ms |
1116 KB |
Output is correct |
19 |
Correct |
9 ms |
1116 KB |
Output is correct |
20 |
Correct |
12 ms |
1308 KB |
Output is correct |
21 |
Correct |
16 ms |
1368 KB |
Output is correct |
22 |
Correct |
21 ms |
1352 KB |
Output is correct |
23 |
Correct |
25 ms |
1448 KB |
Output is correct |
24 |
Correct |
26 ms |
1620 KB |
Output is correct |
25 |
Correct |
33 ms |
1620 KB |
Output is correct |
26 |
Correct |
32 ms |
1620 KB |
Output is correct |
27 |
Correct |
32 ms |
1628 KB |
Output is correct |
28 |
Correct |
33 ms |
1624 KB |
Output is correct |
29 |
Correct |
32 ms |
1616 KB |
Output is correct |
30 |
Incorrect |
154 ms |
2248 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
808 KB |
Output is correct |
16 |
Correct |
3 ms |
1112 KB |
Output is correct |
17 |
Correct |
5 ms |
856 KB |
Output is correct |
18 |
Correct |
8 ms |
1116 KB |
Output is correct |
19 |
Correct |
9 ms |
1116 KB |
Output is correct |
20 |
Correct |
12 ms |
1308 KB |
Output is correct |
21 |
Correct |
16 ms |
1368 KB |
Output is correct |
22 |
Correct |
21 ms |
1352 KB |
Output is correct |
23 |
Correct |
25 ms |
1448 KB |
Output is correct |
24 |
Correct |
26 ms |
1620 KB |
Output is correct |
25 |
Correct |
33 ms |
1620 KB |
Output is correct |
26 |
Correct |
32 ms |
1620 KB |
Output is correct |
27 |
Correct |
32 ms |
1628 KB |
Output is correct |
28 |
Correct |
33 ms |
1624 KB |
Output is correct |
29 |
Correct |
32 ms |
1616 KB |
Output is correct |
30 |
Incorrect |
154 ms |
2248 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |