#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 <= 300; ++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]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
340 KB |
Output is correct |
3 |
Incorrect |
7 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |