#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
int main() {
// std::ios_base::sync_with_stdio(false);
// std::cin.tie(nullptr);
ll n, m;
std::cin >> n >> m;
std::vector<std::vector<std::pair<ll, ll>>> adj(n + m + 1);
std::vector<ll> arr;
for (ll i = 2, p, c; i <= n + m; ++ i) {
std::cin >> p >> c;
arr.push_back(c);
adj[i].push_back({p, c});
adj[p].push_back({i, c});
}
if (n == 1) {
std::sort(arr.begin(), arr.end());
ll ans = 0;
for (auto &i : arr) {
ans += std::abs(arr[arr.size() / 2] - i);
}
std::cout << ans << "\n";
return 0;
}
if (n + m <= 300) {
std::vector<ll> nodes;
std::vector<ll> pars(n + m + 1);
std::function<void(ll, ll)> topological_sort;
topological_sort = [&](ll node, ll par) {
pars[node] = par;
for (auto &i : adj[node]) {
if (i.first == par) continue;
topological_sort(i.first, node);
}
nodes.push_back(node);
};
topological_sort(1, 0);
std::vector<std::vector<ll>> dp(n + m + 1, std::vector<ll>(301));
for (ll val = 0; val <= 300; ++ val) {
for (auto &node : nodes) {
ll node_cnt = 0;
for (auto &child : adj[node]) {
if (child.first == pars[node]) continue;
node_cnt++;
ll cur = inf;
for (ll i = 0; i <= val; ++ i) {
cur = std::min(cur, std::abs(child.second - i) + dp[child.first][val - i]);
}
dp[node][val] += cur;
}
if (node_cnt == 0) dp[node][val] = inf * (val != 0);
else if (dp[node][val] > inf) dp[node][val] = inf;
}
}
ll ans = inf;
for (ll val = 0; val <= 300; ++ val) {
ans = std::min(ans, dp[1][val]);
}
std::cout << ans << "\n";
return 0;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
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 |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
604 KB |
Output is correct |
7 |
Correct |
6 ms |
604 KB |
Output is correct |
8 |
Correct |
7 ms |
888 KB |
Output is correct |
9 |
Correct |
7 ms |
860 KB |
Output is correct |
10 |
Correct |
8 ms |
860 KB |
Output is correct |
11 |
Correct |
9 ms |
1036 KB |
Output is correct |
12 |
Correct |
16 ms |
1260 KB |
Output is correct |
13 |
Correct |
11 ms |
1112 KB |
Output is correct |
14 |
Correct |
11 ms |
1196 KB |
Output is correct |
15 |
Correct |
13 ms |
1196 KB |
Output is correct |
16 |
Correct |
12 ms |
1116 KB |
Output is correct |
17 |
Correct |
20 ms |
1204 KB |
Output is correct |
18 |
Correct |
12 ms |
1116 KB |
Output is correct |
19 |
Correct |
13 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
600 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
6 ms |
604 KB |
Output is correct |
18 |
Correct |
7 ms |
888 KB |
Output is correct |
19 |
Correct |
7 ms |
860 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
9 ms |
1036 KB |
Output is correct |
22 |
Correct |
16 ms |
1260 KB |
Output is correct |
23 |
Correct |
11 ms |
1112 KB |
Output is correct |
24 |
Correct |
11 ms |
1196 KB |
Output is correct |
25 |
Correct |
13 ms |
1196 KB |
Output is correct |
26 |
Correct |
12 ms |
1116 KB |
Output is correct |
27 |
Correct |
20 ms |
1204 KB |
Output is correct |
28 |
Correct |
12 ms |
1116 KB |
Output is correct |
29 |
Correct |
13 ms |
1116 KB |
Output is correct |
30 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
600 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
6 ms |
604 KB |
Output is correct |
18 |
Correct |
7 ms |
888 KB |
Output is correct |
19 |
Correct |
7 ms |
860 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
9 ms |
1036 KB |
Output is correct |
22 |
Correct |
16 ms |
1260 KB |
Output is correct |
23 |
Correct |
11 ms |
1112 KB |
Output is correct |
24 |
Correct |
11 ms |
1196 KB |
Output is correct |
25 |
Correct |
13 ms |
1196 KB |
Output is correct |
26 |
Correct |
12 ms |
1116 KB |
Output is correct |
27 |
Correct |
20 ms |
1204 KB |
Output is correct |
28 |
Correct |
12 ms |
1116 KB |
Output is correct |
29 |
Correct |
13 ms |
1116 KB |
Output is correct |
30 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |