#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 305;
const ll inf = 2e18;
vector<array<int, 2>> adj[N];
ll dp[N][N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
n += m;
for (int i = 2; i <= n; i++) {
int p, c;
cin >> p >> c;
adj[p].push_back({i, c});
}
for (int i = n; i >= 1; i--) {
for (int j = 0; j < N; j++) {
if (adj[i].empty() && j) dp[i][j] = inf;
for (auto [nxt, w] : adj[i]) {
ll mn = 1e18;
for (int k = 0; k <= j; k++) {
mn = min(mn, dp[nxt][k] + abs(j - k - w));
}
dp[i][j] += mn;
dp[i][j] = min(dp[i][j], inf);
}
}
ll mn = *min_element(dp[i], dp[i] + N);
for (int j = N - 1; j >= 0; j--) {
if (dp[i][j] == mn) break;
dp[i][j] = inf;
}
}
cout << *min_element(dp[1], dp[1] + N) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
8 ms |
600 KB |
Output is correct |
8 |
Correct |
9 ms |
860 KB |
Output is correct |
9 |
Correct |
12 ms |
860 KB |
Output is correct |
10 |
Correct |
11 ms |
764 KB |
Output is correct |
11 |
Correct |
15 ms |
860 KB |
Output is correct |
12 |
Correct |
13 ms |
1024 KB |
Output is correct |
13 |
Correct |
14 ms |
1116 KB |
Output is correct |
14 |
Correct |
15 ms |
1116 KB |
Output is correct |
15 |
Correct |
15 ms |
1116 KB |
Output is correct |
16 |
Correct |
15 ms |
1116 KB |
Output is correct |
17 |
Correct |
15 ms |
1140 KB |
Output is correct |
18 |
Correct |
15 ms |
1112 KB |
Output is correct |
19 |
Correct |
16 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |