#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];
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 < 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){
ll mini = 1e9;
for(int j = 0; j < MAXN; ++j){
if(x.second + i - j >= 0) 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);
ll ans = 1e9;
for(int i = 0; i < MAXN; ++i) ans = min(ans, dp[0][i]);
for(int i = 0; i < MAXN; ++i){
if(dp[0][i] == ans){
for(int j = 0; j < i; ++j) assert(dp[0][j] >= dp[0][j + 1]);
for(int j = i; j < n - 1; ++j) assert(dp[0][j] <= dp[0][j + 1]);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
436 KB |
Output is correct |
3 |
Correct |
18 ms |
536 KB |
Output is correct |
4 |
Correct |
21 ms |
688 KB |
Output is correct |
5 |
Correct |
28 ms |
768 KB |
Output is correct |
6 |
Correct |
34 ms |
772 KB |
Output is correct |
7 |
Correct |
40 ms |
852 KB |
Output is correct |
8 |
Correct |
42 ms |
1004 KB |
Output is correct |
9 |
Correct |
44 ms |
1036 KB |
Output is correct |
10 |
Correct |
50 ms |
1116 KB |
Output is correct |
11 |
Correct |
66 ms |
1120 KB |
Output is correct |
12 |
Correct |
61 ms |
1252 KB |
Output is correct |
13 |
Correct |
63 ms |
1404 KB |
Output is correct |
14 |
Correct |
67 ms |
1424 KB |
Output is correct |
15 |
Correct |
70 ms |
1412 KB |
Output is correct |
16 |
Correct |
71 ms |
1436 KB |
Output is correct |
17 |
Correct |
85 ms |
1424 KB |
Output is correct |
18 |
Correct |
70 ms |
1468 KB |
Output is correct |
19 |
Correct |
70 ms |
1488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |