#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e2 + 10;
int n, m;
vector <pair <int, int> > g[N];
int f[N][N];
int cur[N];
void dfs(int u, int p) {
if (g[u].empty()) {
memset(f[u], 60, sizeof(f[u]));
f[u][0] = 0;
}
for (auto [v, w]: g[u]) {
if (v == p) continue;
dfs(v, u);
memset(cur, 60, sizeof(cur));
for (int i = 0; i <= 300; ++i) {
for (int j = 0; j <= i; ++j)
cur[i] = min(cur[i], f[v][j] + abs(i - (w + j)));
}
for (int i = 0; i <= 300; ++i) f[u][i] += cur[i];
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
n += m;
for (int v = 2; v <= n; ++v) {
int u, w;
cin >> u >> w;
g[u].push_back({v, w});
}
dfs(1, 0);
cout << *min_element(f[1], f[1] + 300 + 1);
}
Compilation message
fireworks.cpp: In function 'void dfs(int, int)':
fireworks.cpp:25:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
25 | for (auto [v, w]: g[u]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
336 KB |
Output is correct |
5 |
Correct |
7 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
468 KB |
Output is correct |
7 |
Correct |
8 ms |
532 KB |
Output is correct |
8 |
Correct |
9 ms |
468 KB |
Output is correct |
9 |
Correct |
10 ms |
540 KB |
Output is correct |
10 |
Correct |
11 ms |
596 KB |
Output is correct |
11 |
Correct |
13 ms |
596 KB |
Output is correct |
12 |
Correct |
13 ms |
648 KB |
Output is correct |
13 |
Correct |
14 ms |
596 KB |
Output is correct |
14 |
Correct |
15 ms |
648 KB |
Output is correct |
15 |
Correct |
15 ms |
696 KB |
Output is correct |
16 |
Correct |
14 ms |
632 KB |
Output is correct |
17 |
Correct |
16 ms |
680 KB |
Output is correct |
18 |
Correct |
20 ms |
696 KB |
Output is correct |
19 |
Correct |
17 ms |
732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |