//#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) {
for (auto [to, len] : edge[v]) {
if (to == p) {
continue;
}
if (del[to]) {
need[v] = true;
b[v] -= len;
++k[v];
q[v].push(len);
q[v].push(len);
}
else {
solve(to, v);
if (!need[to]) {
continue;
}
need[v] = true;
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:85:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | for (auto [to, len] : edge[v]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20824 KB |
Output is correct |
3 |
Correct |
5 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20828 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20824 KB |
Output is correct |
8 |
Correct |
4 ms |
20840 KB |
Output is correct |
9 |
Correct |
5 ms |
20944 KB |
Output is correct |
10 |
Correct |
4 ms |
20824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 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 |
20828 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20828 KB |
Output is correct |
8 |
Correct |
5 ms |
20824 KB |
Output is correct |
9 |
Correct |
6 ms |
20828 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
5 ms |
20828 KB |
Output is correct |
13 |
Correct |
4 ms |
20776 KB |
Output is correct |
14 |
Correct |
5 ms |
20828 KB |
Output is correct |
15 |
Correct |
5 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 |
6 ms |
20672 KB |
Output is correct |
19 |
Correct |
5 ms |
20828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20824 KB |
Output is correct |
3 |
Correct |
5 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20828 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20824 KB |
Output is correct |
8 |
Correct |
4 ms |
20840 KB |
Output is correct |
9 |
Correct |
5 ms |
20944 KB |
Output is correct |
10 |
Correct |
4 ms |
20824 KB |
Output is correct |
11 |
Correct |
5 ms |
20828 KB |
Output is correct |
12 |
Correct |
4 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 |
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 |
5 ms |
20824 KB |
Output is correct |
19 |
Correct |
6 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
5 ms |
20828 KB |
Output is correct |
23 |
Correct |
4 ms |
20776 KB |
Output is correct |
24 |
Correct |
5 ms |
20828 KB |
Output is correct |
25 |
Correct |
5 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 |
6 ms |
20672 KB |
Output is correct |
29 |
Correct |
5 ms |
20828 KB |
Output is correct |
30 |
Correct |
6 ms |
20828 KB |
Output is correct |
31 |
Correct |
6 ms |
21084 KB |
Output is correct |
32 |
Correct |
6 ms |
21084 KB |
Output is correct |
33 |
Correct |
8 ms |
21352 KB |
Output is correct |
34 |
Correct |
8 ms |
21596 KB |
Output is correct |
35 |
Correct |
12 ms |
22364 KB |
Output is correct |
36 |
Correct |
11 ms |
22108 KB |
Output is correct |
37 |
Correct |
10 ms |
22112 KB |
Output is correct |
38 |
Correct |
24 ms |
23380 KB |
Output is correct |
39 |
Correct |
12 ms |
22360 KB |
Output is correct |
40 |
Correct |
6 ms |
21848 KB |
Output is correct |
41 |
Correct |
7 ms |
21848 KB |
Output is correct |
42 |
Correct |
6 ms |
21088 KB |
Output is correct |
43 |
Correct |
184 ms |
52252 KB |
Output is correct |
44 |
Correct |
145 ms |
44156 KB |
Output is correct |
45 |
Correct |
147 ms |
43548 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
7 ms |
21072 KB |
Output is correct |
48 |
Correct |
7 ms |
21080 KB |
Output is correct |
49 |
Correct |
6 ms |
21080 KB |
Output is correct |
50 |
Correct |
6 ms |
21084 KB |
Output is correct |
51 |
Correct |
6 ms |
21084 KB |
Output is correct |
52 |
Correct |
12 ms |
21596 KB |
Output is correct |
53 |
Correct |
7 ms |
21340 KB |
Output is correct |
54 |
Correct |
6 ms |
21296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20824 KB |
Output is correct |
3 |
Correct |
5 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20828 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
5 ms |
20824 KB |
Output is correct |
8 |
Correct |
4 ms |
20840 KB |
Output is correct |
9 |
Correct |
5 ms |
20944 KB |
Output is correct |
10 |
Correct |
4 ms |
20824 KB |
Output is correct |
11 |
Correct |
5 ms |
20828 KB |
Output is correct |
12 |
Correct |
4 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 |
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 |
5 ms |
20824 KB |
Output is correct |
19 |
Correct |
6 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
5 ms |
20828 KB |
Output is correct |
23 |
Correct |
4 ms |
20776 KB |
Output is correct |
24 |
Correct |
5 ms |
20828 KB |
Output is correct |
25 |
Correct |
5 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 |
6 ms |
20672 KB |
Output is correct |
29 |
Correct |
5 ms |
20828 KB |
Output is correct |
30 |
Correct |
6 ms |
20828 KB |
Output is correct |
31 |
Correct |
6 ms |
21084 KB |
Output is correct |
32 |
Correct |
6 ms |
21084 KB |
Output is correct |
33 |
Correct |
8 ms |
21352 KB |
Output is correct |
34 |
Correct |
8 ms |
21596 KB |
Output is correct |
35 |
Correct |
12 ms |
22364 KB |
Output is correct |
36 |
Correct |
11 ms |
22108 KB |
Output is correct |
37 |
Correct |
10 ms |
22112 KB |
Output is correct |
38 |
Correct |
24 ms |
23380 KB |
Output is correct |
39 |
Correct |
12 ms |
22360 KB |
Output is correct |
40 |
Correct |
6 ms |
21848 KB |
Output is correct |
41 |
Correct |
7 ms |
21848 KB |
Output is correct |
42 |
Correct |
6 ms |
21088 KB |
Output is correct |
43 |
Correct |
184 ms |
52252 KB |
Output is correct |
44 |
Correct |
145 ms |
44156 KB |
Output is correct |
45 |
Correct |
147 ms |
43548 KB |
Output is correct |
46 |
Correct |
6 ms |
21084 KB |
Output is correct |
47 |
Correct |
7 ms |
21072 KB |
Output is correct |
48 |
Correct |
7 ms |
21080 KB |
Output is correct |
49 |
Correct |
6 ms |
21080 KB |
Output is correct |
50 |
Correct |
6 ms |
21084 KB |
Output is correct |
51 |
Correct |
6 ms |
21084 KB |
Output is correct |
52 |
Correct |
12 ms |
21596 KB |
Output is correct |
53 |
Correct |
7 ms |
21340 KB |
Output is correct |
54 |
Correct |
6 ms |
21296 KB |
Output is correct |
55 |
Correct |
43 ms |
27724 KB |
Output is correct |
56 |
Correct |
384 ms |
74032 KB |
Output is correct |
57 |
Correct |
799 ms |
125916 KB |
Output is correct |
58 |
Correct |
921 ms |
144492 KB |
Output is correct |
59 |
Execution timed out |
2082 ms |
282176 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |