#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
708 KB |
Output is correct |
6 |
Correct |
5 ms |
832 KB |
Output is correct |
7 |
Correct |
5 ms |
832 KB |
Output is correct |
8 |
Correct |
7 ms |
972 KB |
Output is correct |
9 |
Correct |
15 ms |
1048 KB |
Output is correct |
10 |
Correct |
13 ms |
1164 KB |
Output is correct |
11 |
Correct |
16 ms |
1136 KB |
Output is correct |
12 |
Correct |
21 ms |
1352 KB |
Output is correct |
13 |
Correct |
24 ms |
1356 KB |
Output is correct |
14 |
Correct |
26 ms |
1484 KB |
Output is correct |
15 |
Correct |
46 ms |
1480 KB |
Output is correct |
16 |
Correct |
44 ms |
1388 KB |
Output is correct |
17 |
Correct |
33 ms |
1484 KB |
Output is correct |
18 |
Correct |
34 ms |
1464 KB |
Output is correct |
19 |
Correct |
32 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
708 KB |
Output is correct |
16 |
Correct |
5 ms |
832 KB |
Output is correct |
17 |
Correct |
5 ms |
832 KB |
Output is correct |
18 |
Correct |
7 ms |
972 KB |
Output is correct |
19 |
Correct |
15 ms |
1048 KB |
Output is correct |
20 |
Correct |
13 ms |
1164 KB |
Output is correct |
21 |
Correct |
16 ms |
1136 KB |
Output is correct |
22 |
Correct |
21 ms |
1352 KB |
Output is correct |
23 |
Correct |
24 ms |
1356 KB |
Output is correct |
24 |
Correct |
26 ms |
1484 KB |
Output is correct |
25 |
Correct |
46 ms |
1480 KB |
Output is correct |
26 |
Correct |
44 ms |
1388 KB |
Output is correct |
27 |
Correct |
33 ms |
1484 KB |
Output is correct |
28 |
Correct |
34 ms |
1464 KB |
Output is correct |
29 |
Correct |
32 ms |
1484 KB |
Output is correct |
30 |
Incorrect |
162 ms |
2148 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
708 KB |
Output is correct |
16 |
Correct |
5 ms |
832 KB |
Output is correct |
17 |
Correct |
5 ms |
832 KB |
Output is correct |
18 |
Correct |
7 ms |
972 KB |
Output is correct |
19 |
Correct |
15 ms |
1048 KB |
Output is correct |
20 |
Correct |
13 ms |
1164 KB |
Output is correct |
21 |
Correct |
16 ms |
1136 KB |
Output is correct |
22 |
Correct |
21 ms |
1352 KB |
Output is correct |
23 |
Correct |
24 ms |
1356 KB |
Output is correct |
24 |
Correct |
26 ms |
1484 KB |
Output is correct |
25 |
Correct |
46 ms |
1480 KB |
Output is correct |
26 |
Correct |
44 ms |
1388 KB |
Output is correct |
27 |
Correct |
33 ms |
1484 KB |
Output is correct |
28 |
Correct |
34 ms |
1464 KB |
Output is correct |
29 |
Correct |
32 ms |
1484 KB |
Output is correct |
30 |
Incorrect |
162 ms |
2148 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |