//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = 3e5 + 11, inf = 1000111222;
vector <pii> edge[max_n];
int n, m;
ll ans = 0;
bool del[max_n];
inline bool dfs (int v, int p = -1, bool gg = false) {
bool ok = v >= n || gg;
bool there = v >= n;
for (auto [to, len] : edge[v]) {
if (to == p) {
continue;
}
if (dfs(to, v, ok)) {
there = true;
if (ok) {
ans += len;
}
}
}
if (v >= n && !gg) {
del[v] = true;
}
return there;
}
bool need[max_n];
priority_queue <ll> q[max_n];
ll b[max_n];
int k[max_n];
inline void solve (int v, int p = -1) {
int mx = -1, big = -1, L = -1;
for (auto [to, len] : edge[v]) {
if (to == p) {
continue;
}
if (del[to]) {
need[v] = true;
if (umax(mx, 0)) {
big = to;
L = len;
}
}
else {
solve(to, v);
if (!need[to]) {
continue;
}
need[v] = true;
if (umax(mx, len(q[to]))) {
big = to;
L = len;
}
}
}
if (!need[v]) {
return;
}
assert(big != -1);
{
int to = big, len = L;
if (del[to]) {
b[v] -= len;
++k[v];
q[v].push(len);
q[v].push(len);
} else {
int to_del = k[to] - 1;
ll s = 0;
for (int i = 0; i < to_del; i++) {
s += q[to].top();
q[to].pop();
}
ll a2 = q[to].top() + len;
q[to].pop();
ll a1 = q[to].top() + len;
q[to].pop();
q[v].swap(q[to]);
q[v].push(a1);
q[v].push(a2);
b[v] += b[to] - len + s;
++k[v];
}
}
for (auto [to, len] : edge[v]) {
if (to == p || to == big) {
continue;
}
if (del[to]) {
b[v] -= len;
++k[v];
q[v].push(len);
q[v].push(len);
}
else {
if (!need[to]) {
continue;
}
int to_del = k[to] - 1;
ll s = 0;
for (int i = 0; i < to_del; i++) {
s += q[to].top();
q[to].pop();
}
ll a2 = q[to].top() + len;
q[to].pop();
ll a1 = q[to].top() + len;
q[to].pop();
while (!q[to].empty()) {
q[v].push(q[to].top());
q[to].pop();
}
q[v].push(a1);
q[v].push(a2);
b[v] += b[to] - len + s;
++k[v];
}
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1, p, c; i < n + m; i++) {
cin >> p >> c;
--p;
edge[p].pb({i, c});
edge[i].pb({p, c});
}
dfs(0);
solve(0);
while (k[0] > 0) {
b[0] += q[0].top();
q[0].pop();
--k[0];
}
assert(!q[0].empty());
cout << b[0] + ans << '\n';
}
Compilation message
fireworks.cpp: In function 'bool dfs(int, int, bool)':
fireworks.cpp:61:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for (auto [to, len] : edge[v]) {
| ^
fireworks.cpp: In function 'void solve(int, int)':
fireworks.cpp:86:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for (auto [to, len] : edge[v]) {
| ^
fireworks.cpp:138:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
138 | for (auto [to, len] : edge[v]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20824 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
7 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20828 KB |
Output is correct |
8 |
Correct |
5 ms |
20828 KB |
Output is correct |
9 |
Correct |
5 ms |
21080 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
6 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20824 KB |
Output is correct |
6 |
Correct |
5 ms |
20824 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
4 ms |
20828 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
5 ms |
20824 KB |
Output is correct |
12 |
Correct |
4 ms |
20828 KB |
Output is correct |
13 |
Correct |
5 ms |
20828 KB |
Output is correct |
14 |
Correct |
4 ms |
20824 KB |
Output is correct |
15 |
Correct |
4 ms |
20828 KB |
Output is correct |
16 |
Correct |
5 ms |
20828 KB |
Output is correct |
17 |
Correct |
5 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20824 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
7 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20828 KB |
Output is correct |
8 |
Correct |
5 ms |
20828 KB |
Output is correct |
9 |
Correct |
5 ms |
21080 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20824 KB |
Output is correct |
12 |
Correct |
6 ms |
20828 KB |
Output is correct |
13 |
Correct |
4 ms |
20828 KB |
Output is correct |
14 |
Correct |
5 ms |
20828 KB |
Output is correct |
15 |
Correct |
5 ms |
20824 KB |
Output is correct |
16 |
Correct |
5 ms |
20824 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
5 ms |
20824 KB |
Output is correct |
22 |
Correct |
4 ms |
20828 KB |
Output is correct |
23 |
Correct |
5 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
20824 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
5 ms |
20828 KB |
Output is correct |
27 |
Correct |
5 ms |
20828 KB |
Output is correct |
28 |
Correct |
4 ms |
20828 KB |
Output is correct |
29 |
Correct |
4 ms |
20824 KB |
Output is correct |
30 |
Correct |
5 ms |
20824 KB |
Output is correct |
31 |
Correct |
6 ms |
20824 KB |
Output is correct |
32 |
Correct |
5 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
20828 KB |
Output is correct |
34 |
Correct |
6 ms |
20832 KB |
Output is correct |
35 |
Correct |
5 ms |
20824 KB |
Output is correct |
36 |
Correct |
7 ms |
20828 KB |
Output is correct |
37 |
Correct |
8 ms |
21084 KB |
Output is correct |
38 |
Correct |
8 ms |
20920 KB |
Output is correct |
39 |
Correct |
8 ms |
21084 KB |
Output is correct |
40 |
Correct |
6 ms |
21852 KB |
Output is correct |
41 |
Correct |
6 ms |
21852 KB |
Output is correct |
42 |
Correct |
5 ms |
20828 KB |
Output is correct |
43 |
Correct |
9 ms |
21596 KB |
Output is correct |
44 |
Correct |
6 ms |
21432 KB |
Output is correct |
45 |
Correct |
6 ms |
21340 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
6 ms |
21080 KB |
Output is correct |
48 |
Correct |
6 ms |
21084 KB |
Output is correct |
49 |
Correct |
6 ms |
21084 KB |
Output is correct |
50 |
Correct |
6 ms |
20952 KB |
Output is correct |
51 |
Correct |
7 ms |
21084 KB |
Output is correct |
52 |
Correct |
6 ms |
21084 KB |
Output is correct |
53 |
Correct |
6 ms |
21084 KB |
Output is correct |
54 |
Correct |
7 ms |
21080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20824 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
7 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20828 KB |
Output is correct |
8 |
Correct |
5 ms |
20828 KB |
Output is correct |
9 |
Correct |
5 ms |
21080 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20824 KB |
Output is correct |
12 |
Correct |
6 ms |
20828 KB |
Output is correct |
13 |
Correct |
4 ms |
20828 KB |
Output is correct |
14 |
Correct |
5 ms |
20828 KB |
Output is correct |
15 |
Correct |
5 ms |
20824 KB |
Output is correct |
16 |
Correct |
5 ms |
20824 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
5 ms |
20824 KB |
Output is correct |
22 |
Correct |
4 ms |
20828 KB |
Output is correct |
23 |
Correct |
5 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
20824 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
5 ms |
20828 KB |
Output is correct |
27 |
Correct |
5 ms |
20828 KB |
Output is correct |
28 |
Correct |
4 ms |
20828 KB |
Output is correct |
29 |
Correct |
4 ms |
20824 KB |
Output is correct |
30 |
Correct |
5 ms |
20824 KB |
Output is correct |
31 |
Correct |
6 ms |
20824 KB |
Output is correct |
32 |
Correct |
5 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
20828 KB |
Output is correct |
34 |
Correct |
6 ms |
20832 KB |
Output is correct |
35 |
Correct |
5 ms |
20824 KB |
Output is correct |
36 |
Correct |
7 ms |
20828 KB |
Output is correct |
37 |
Correct |
8 ms |
21084 KB |
Output is correct |
38 |
Correct |
8 ms |
20920 KB |
Output is correct |
39 |
Correct |
8 ms |
21084 KB |
Output is correct |
40 |
Correct |
6 ms |
21852 KB |
Output is correct |
41 |
Correct |
6 ms |
21852 KB |
Output is correct |
42 |
Correct |
5 ms |
20828 KB |
Output is correct |
43 |
Correct |
9 ms |
21596 KB |
Output is correct |
44 |
Correct |
6 ms |
21432 KB |
Output is correct |
45 |
Correct |
6 ms |
21340 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
6 ms |
21080 KB |
Output is correct |
48 |
Correct |
6 ms |
21084 KB |
Output is correct |
49 |
Correct |
6 ms |
21084 KB |
Output is correct |
50 |
Correct |
6 ms |
20952 KB |
Output is correct |
51 |
Correct |
7 ms |
21084 KB |
Output is correct |
52 |
Correct |
6 ms |
21084 KB |
Output is correct |
53 |
Correct |
6 ms |
21084 KB |
Output is correct |
54 |
Correct |
7 ms |
21080 KB |
Output is correct |
55 |
Correct |
9 ms |
21600 KB |
Output is correct |
56 |
Correct |
26 ms |
23772 KB |
Output is correct |
57 |
Correct |
44 ms |
25688 KB |
Output is correct |
58 |
Correct |
70 ms |
27056 KB |
Output is correct |
59 |
Correct |
97 ms |
29136 KB |
Output is correct |
60 |
Correct |
139 ms |
33932 KB |
Output is correct |
61 |
Correct |
150 ms |
36224 KB |
Output is correct |
62 |
Correct |
162 ms |
37832 KB |
Output is correct |
63 |
Correct |
195 ms |
40500 KB |
Output is correct |
64 |
Correct |
212 ms |
41408 KB |
Output is correct |
65 |
Correct |
117 ms |
96080 KB |
Output is correct |
66 |
Correct |
140 ms |
96084 KB |
Output is correct |
67 |
Correct |
97 ms |
35412 KB |
Output is correct |
68 |
Correct |
134 ms |
70204 KB |
Output is correct |
69 |
Correct |
146 ms |
65216 KB |
Output is correct |
70 |
Correct |
151 ms |
65064 KB |
Output is correct |
71 |
Correct |
125 ms |
51544 KB |
Output is correct |
72 |
Correct |
126 ms |
52996 KB |
Output is correct |
73 |
Correct |
108 ms |
49340 KB |
Output is correct |
74 |
Correct |
110 ms |
48556 KB |
Output is correct |
75 |
Correct |
112 ms |
46940 KB |
Output is correct |
76 |
Correct |
110 ms |
48072 KB |
Output is correct |
77 |
Correct |
118 ms |
46464 KB |
Output is correct |
78 |
Correct |
118 ms |
44492 KB |
Output is correct |
79 |
Correct |
106 ms |
45088 KB |
Output is correct |