#include <bits/stdc++.h>
#define nl '\n'
#define int int64_t
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
const int inf = 1e9 + 7;
std::vector<int> pa(n + m), co(n + m);
std::vector<std::vector<std::pair<int, int>>> adj(n + m);
for (int i = 1; i < n + m; i++) {
std::cin >> pa[i] >> co[i];
adj[--pa[i]].emplace_back(i, co[i]);
}
std::vector<std::vector<int>> dp(n + m, std::vector<int>(301));
auto dfs = [&](auto self, int u, int p) -> void {
if (adj[u].size() == 0) {
dp[u][0] = 0;
for (int i = 1; i <= 300; i++) {
dp[u][i] = inf;
}
return;
}
for (auto [v, c] : adj[u]) {
self(self, v, u);
}
for (int j = 0; j <= 300; j++) {
dp[u][j] = 0;
for (auto [v, c] : adj[u]) {
int minimal = inf;
for (int k = 0; k <= j; k++) {
minimal = std::min(minimal, dp[v][k] + std::abs(j - k - c));
}
dp[u][j] += minimal;
}
dp[u][j] = std::min(dp[u][j], inf);
}
};
dfs(dfs, 0, 0);
std::cout << *std::min_element(dp[0].begin(), dp[0].end()) << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
588 KB |
Output is correct |
4 |
Correct |
5 ms |
692 KB |
Output is correct |
5 |
Correct |
6 ms |
760 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 |
9 ms |
924 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
11 |
Correct |
12 ms |
860 KB |
Output is correct |
12 |
Correct |
16 ms |
1088 KB |
Output is correct |
13 |
Correct |
13 ms |
1116 KB |
Output is correct |
14 |
Correct |
15 ms |
1116 KB |
Output is correct |
15 |
Correct |
14 ms |
1112 KB |
Output is correct |
16 |
Correct |
15 ms |
1368 KB |
Output is correct |
17 |
Correct |
15 ms |
1112 KB |
Output is correct |
18 |
Correct |
15 ms |
1116 KB |
Output is correct |
19 |
Correct |
15 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |