#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;
template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
int main() {
int n, m; scanf("%d%d", &n, &m);
std::vector<int> leaf(m);
std::vector<std::vector<std::pair<int, i64>>> g(n + m); n += m;
for(int i = 1; i < n; i++) {
int a; i64 b; scanf("%d%lld", &a, &b);
// g[i].push_back({a - 1, b});
g[a - 1].push_back({i, b});
}
std::vector<i64> dist(n, 0);
std::function<void (int, int, i64)> dfs = [&](int v, int par, i64 ari) {
dist[v] = ari;
for(auto e: g[v]) {
if(e.first == par) continue;
dfs(e.first, v, ari + e.second);
}
};
dfs(0, -1, 0);
i64 ans = 1LL << 60;
if(n - m == 1) {
std::function<i64 (std::vector<i64>&)> OSNIKTR = [](std::vector<i64>& vec) {
i64 X = 0, Y = 1LL << 60;
std::vector<i64> s(vec.size() + 1, 0);
for(int i = 0; i < vec.size(); i++) s[i + 1] = s[i] + vec[i];
for(i64 i = 0; i < vec.size(); i++) {
i64 tmp = 0;
tmp += i * vec[i] - s[i];
tmp += (s.back() - s[i]) - ((i64)vec.size() - i) * vec[i];
if(tmp < Y) {
Y = tmp;
X = vec[i];
}
}
return X;
};
sort(begin(dist), end(dist));
dist.erase(begin(dist));
i64 makino = OSNIKTR(dist);
ans = 0;
for(auto v: dist) ans += std::abs(v - makino);
} else if(n <= 300) {
auto dp = make_v<i64>(n, 301);
for(int i = 0; i < dp.size(); i++) for(int j = 0; j < dp[i].size(); j++) dp[i][j] = 1LL << 60;
std::function<void (int)> solve = [&](int v) {
if(v > n - m - 1) {
dp[v][0] = 0;
return;
}
for(auto e: g[v]) solve(e.first);
for(int i = 0; i < dp[v].size(); i++) {
i64 tmp = 0;
for(auto e: g[v]) {
i64 mi = 1LL << 60;
for(int j = 0; j < dp[e.first].size(); j++) {
i64 diff = i - (j + e.second);
if(e.second + diff < 0) continue;
mi = std::min(mi, dp[e.first][j] + std::abs(diff));
}
tmp += mi;
}
dp[v][i] = tmp;
}
};
solve(0);
for(auto v: dp[0]) ans = std::min(ans, v);
} else {
assert(0);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
fireworks.cpp: In lambda function:
fireworks.cpp:42:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < vec.size(); i++) s[i + 1] = s[i] + vec[i];
~~^~~~~~~~~~~~
fireworks.cpp:44:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i64 i = 0; i < vec.size(); i++) {
~~^~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:64:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < dp.size(); i++) for(int j = 0; j < dp[i].size(); j++) dp[i][j] = 1LL << 60;
~~^~~~~~~~~~~
fireworks.cpp:64:55: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < dp.size(); i++) for(int j = 0; j < dp[i].size(); j++) dp[i][j] = 1LL << 60;
~~^~~~~~~~~~~~~~
fireworks.cpp: In lambda function:
fireworks.cpp:72:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < dp[v].size(); i++) {
~~^~~~~~~~~~~~~~
fireworks.cpp:76:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < dp[e.first].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:17:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, m; scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:21:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a; i64 b; scanf("%d%lld", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
11 ms |
384 KB |
Output is correct |
4 |
Correct |
20 ms |
640 KB |
Output is correct |
5 |
Correct |
16 ms |
640 KB |
Output is correct |
6 |
Correct |
15 ms |
640 KB |
Output is correct |
7 |
Correct |
22 ms |
780 KB |
Output is correct |
8 |
Correct |
30 ms |
768 KB |
Output is correct |
9 |
Correct |
26 ms |
888 KB |
Output is correct |
10 |
Correct |
27 ms |
896 KB |
Output is correct |
11 |
Correct |
39 ms |
896 KB |
Output is correct |
12 |
Correct |
37 ms |
1024 KB |
Output is correct |
13 |
Correct |
33 ms |
1024 KB |
Output is correct |
14 |
Correct |
41 ms |
1100 KB |
Output is correct |
15 |
Correct |
41 ms |
1024 KB |
Output is correct |
16 |
Correct |
46 ms |
1072 KB |
Output is correct |
17 |
Correct |
40 ms |
1024 KB |
Output is correct |
18 |
Correct |
32 ms |
1116 KB |
Output is correct |
19 |
Correct |
31 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
460 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
11 ms |
384 KB |
Output is correct |
14 |
Correct |
20 ms |
640 KB |
Output is correct |
15 |
Correct |
16 ms |
640 KB |
Output is correct |
16 |
Correct |
15 ms |
640 KB |
Output is correct |
17 |
Correct |
22 ms |
780 KB |
Output is correct |
18 |
Correct |
30 ms |
768 KB |
Output is correct |
19 |
Correct |
26 ms |
888 KB |
Output is correct |
20 |
Correct |
27 ms |
896 KB |
Output is correct |
21 |
Correct |
39 ms |
896 KB |
Output is correct |
22 |
Correct |
37 ms |
1024 KB |
Output is correct |
23 |
Correct |
33 ms |
1024 KB |
Output is correct |
24 |
Correct |
41 ms |
1100 KB |
Output is correct |
25 |
Correct |
41 ms |
1024 KB |
Output is correct |
26 |
Correct |
46 ms |
1072 KB |
Output is correct |
27 |
Correct |
40 ms |
1024 KB |
Output is correct |
28 |
Correct |
32 ms |
1116 KB |
Output is correct |
29 |
Correct |
31 ms |
1024 KB |
Output is correct |
30 |
Runtime error |
3 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
460 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
11 ms |
384 KB |
Output is correct |
14 |
Correct |
20 ms |
640 KB |
Output is correct |
15 |
Correct |
16 ms |
640 KB |
Output is correct |
16 |
Correct |
15 ms |
640 KB |
Output is correct |
17 |
Correct |
22 ms |
780 KB |
Output is correct |
18 |
Correct |
30 ms |
768 KB |
Output is correct |
19 |
Correct |
26 ms |
888 KB |
Output is correct |
20 |
Correct |
27 ms |
896 KB |
Output is correct |
21 |
Correct |
39 ms |
896 KB |
Output is correct |
22 |
Correct |
37 ms |
1024 KB |
Output is correct |
23 |
Correct |
33 ms |
1024 KB |
Output is correct |
24 |
Correct |
41 ms |
1100 KB |
Output is correct |
25 |
Correct |
41 ms |
1024 KB |
Output is correct |
26 |
Correct |
46 ms |
1072 KB |
Output is correct |
27 |
Correct |
40 ms |
1024 KB |
Output is correct |
28 |
Correct |
32 ms |
1116 KB |
Output is correct |
29 |
Correct |
31 ms |
1024 KB |
Output is correct |
30 |
Runtime error |
3 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |