// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 3e5 + 10, mod = 1e9 + 7;
const ll inf = 1e18 + 10;
vector<pii> v[maxn];
int n, m;
multiset<ll> peb[maxn];
int infs[maxn];
void dfs(int u, int S = 0){
if(u >= n){
peb[u].insert(S);
peb[u].insert(S);
return;
}
for(auto [y, x] : v[u]){
dfs(y, x);
infs[u]+= infs[y];
if(sz(peb[u]) < sz(peb[y]))
swap(peb[u], peb[y]);
for(ll x : peb[y])
peb[u].insert(x);
}
int N = sz(peb[u]) + infs[u];
assert(sz(peb[u]) >= (N/2)+1);
while(sz(peb[u]) > (N/2)+1)
infs[u]++, peb[u].erase(--peb[u].end());
ll R = *peb[u].rbegin();
peb[u].erase(--peb[u].end());
ll L = *peb[u].rbegin();
peb[u].erase(--peb[u].end());
peb[u].insert(L + S);
peb[u].insert(R + S);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
ll sm = 0;
cin >> n >> m;
for(int i = 1; i < n+m; i++){
int a, b;
cin >> a >> b;
v[--a].PB({i, b});
sm+= b;
}
dfs(0);
int N = sz(peb[0]) + infs[0];
while(sz(peb[0]) > (N/2))
infs[0]++, peb[0].erase(--peb[0].end());
ll bst = *peb[0].rbegin();
ll df = 0;
for(ll x : peb[0]){
df+= abs(x - bst);
df-= x;
}
df+= -bst * infs[0];
ll ans = df + sm *2;
return cout << ans/2 << endl, 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int, int)':
fireworks.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for(auto [y, x] : v[u]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
21504 KB |
Output is correct |
2 |
Correct |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21632 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
14 ms |
21484 KB |
Output is correct |
9 |
Correct |
14 ms |
21504 KB |
Output is correct |
10 |
Correct |
14 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
21504 KB |
Output is correct |
2 |
Correct |
13 ms |
21504 KB |
Output is correct |
3 |
Correct |
13 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
13 ms |
21504 KB |
Output is correct |
6 |
Correct |
14 ms |
21504 KB |
Output is correct |
7 |
Correct |
14 ms |
21504 KB |
Output is correct |
8 |
Correct |
13 ms |
21504 KB |
Output is correct |
9 |
Correct |
14 ms |
21504 KB |
Output is correct |
10 |
Correct |
14 ms |
21504 KB |
Output is correct |
11 |
Correct |
14 ms |
21504 KB |
Output is correct |
12 |
Correct |
14 ms |
21504 KB |
Output is correct |
13 |
Correct |
14 ms |
21504 KB |
Output is correct |
14 |
Correct |
14 ms |
21504 KB |
Output is correct |
15 |
Correct |
14 ms |
21504 KB |
Output is correct |
16 |
Correct |
14 ms |
21504 KB |
Output is correct |
17 |
Correct |
14 ms |
21504 KB |
Output is correct |
18 |
Correct |
14 ms |
21504 KB |
Output is correct |
19 |
Correct |
14 ms |
21632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
21504 KB |
Output is correct |
2 |
Correct |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21632 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
14 ms |
21484 KB |
Output is correct |
9 |
Correct |
14 ms |
21504 KB |
Output is correct |
10 |
Correct |
14 ms |
21504 KB |
Output is correct |
11 |
Correct |
13 ms |
21504 KB |
Output is correct |
12 |
Correct |
13 ms |
21504 KB |
Output is correct |
13 |
Correct |
13 ms |
21504 KB |
Output is correct |
14 |
Correct |
14 ms |
21504 KB |
Output is correct |
15 |
Correct |
13 ms |
21504 KB |
Output is correct |
16 |
Correct |
14 ms |
21504 KB |
Output is correct |
17 |
Correct |
14 ms |
21504 KB |
Output is correct |
18 |
Correct |
13 ms |
21504 KB |
Output is correct |
19 |
Correct |
14 ms |
21504 KB |
Output is correct |
20 |
Correct |
14 ms |
21504 KB |
Output is correct |
21 |
Correct |
14 ms |
21504 KB |
Output is correct |
22 |
Correct |
14 ms |
21504 KB |
Output is correct |
23 |
Correct |
14 ms |
21504 KB |
Output is correct |
24 |
Correct |
14 ms |
21504 KB |
Output is correct |
25 |
Correct |
14 ms |
21504 KB |
Output is correct |
26 |
Correct |
14 ms |
21504 KB |
Output is correct |
27 |
Correct |
14 ms |
21504 KB |
Output is correct |
28 |
Correct |
14 ms |
21504 KB |
Output is correct |
29 |
Correct |
14 ms |
21632 KB |
Output is correct |
30 |
Correct |
14 ms |
21504 KB |
Output is correct |
31 |
Correct |
16 ms |
21632 KB |
Output is correct |
32 |
Correct |
16 ms |
21632 KB |
Output is correct |
33 |
Correct |
15 ms |
21760 KB |
Output is correct |
34 |
Correct |
16 ms |
21760 KB |
Output is correct |
35 |
Correct |
16 ms |
21888 KB |
Output is correct |
36 |
Correct |
16 ms |
21888 KB |
Output is correct |
37 |
Correct |
17 ms |
22016 KB |
Output is correct |
38 |
Correct |
20 ms |
22016 KB |
Output is correct |
39 |
Correct |
17 ms |
22016 KB |
Output is correct |
40 |
Correct |
16 ms |
22144 KB |
Output is correct |
41 |
Correct |
16 ms |
22144 KB |
Output is correct |
42 |
Correct |
16 ms |
21632 KB |
Output is correct |
43 |
Correct |
18 ms |
22144 KB |
Output is correct |
44 |
Correct |
17 ms |
22144 KB |
Output is correct |
45 |
Correct |
17 ms |
22144 KB |
Output is correct |
46 |
Correct |
20 ms |
22400 KB |
Output is correct |
47 |
Correct |
18 ms |
22400 KB |
Output is correct |
48 |
Correct |
18 ms |
22272 KB |
Output is correct |
49 |
Correct |
18 ms |
22400 KB |
Output is correct |
50 |
Correct |
17 ms |
22272 KB |
Output is correct |
51 |
Correct |
17 ms |
22144 KB |
Output is correct |
52 |
Correct |
17 ms |
22272 KB |
Output is correct |
53 |
Correct |
18 ms |
22272 KB |
Output is correct |
54 |
Correct |
18 ms |
22400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
21504 KB |
Output is correct |
2 |
Correct |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21632 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
14 ms |
21484 KB |
Output is correct |
9 |
Correct |
14 ms |
21504 KB |
Output is correct |
10 |
Correct |
14 ms |
21504 KB |
Output is correct |
11 |
Correct |
13 ms |
21504 KB |
Output is correct |
12 |
Correct |
13 ms |
21504 KB |
Output is correct |
13 |
Correct |
13 ms |
21504 KB |
Output is correct |
14 |
Correct |
14 ms |
21504 KB |
Output is correct |
15 |
Correct |
13 ms |
21504 KB |
Output is correct |
16 |
Correct |
14 ms |
21504 KB |
Output is correct |
17 |
Correct |
14 ms |
21504 KB |
Output is correct |
18 |
Correct |
13 ms |
21504 KB |
Output is correct |
19 |
Correct |
14 ms |
21504 KB |
Output is correct |
20 |
Correct |
14 ms |
21504 KB |
Output is correct |
21 |
Correct |
14 ms |
21504 KB |
Output is correct |
22 |
Correct |
14 ms |
21504 KB |
Output is correct |
23 |
Correct |
14 ms |
21504 KB |
Output is correct |
24 |
Correct |
14 ms |
21504 KB |
Output is correct |
25 |
Correct |
14 ms |
21504 KB |
Output is correct |
26 |
Correct |
14 ms |
21504 KB |
Output is correct |
27 |
Correct |
14 ms |
21504 KB |
Output is correct |
28 |
Correct |
14 ms |
21504 KB |
Output is correct |
29 |
Correct |
14 ms |
21632 KB |
Output is correct |
30 |
Correct |
14 ms |
21504 KB |
Output is correct |
31 |
Correct |
16 ms |
21632 KB |
Output is correct |
32 |
Correct |
16 ms |
21632 KB |
Output is correct |
33 |
Correct |
15 ms |
21760 KB |
Output is correct |
34 |
Correct |
16 ms |
21760 KB |
Output is correct |
35 |
Correct |
16 ms |
21888 KB |
Output is correct |
36 |
Correct |
16 ms |
21888 KB |
Output is correct |
37 |
Correct |
17 ms |
22016 KB |
Output is correct |
38 |
Correct |
20 ms |
22016 KB |
Output is correct |
39 |
Correct |
17 ms |
22016 KB |
Output is correct |
40 |
Correct |
16 ms |
22144 KB |
Output is correct |
41 |
Correct |
16 ms |
22144 KB |
Output is correct |
42 |
Correct |
16 ms |
21632 KB |
Output is correct |
43 |
Correct |
18 ms |
22144 KB |
Output is correct |
44 |
Correct |
17 ms |
22144 KB |
Output is correct |
45 |
Correct |
17 ms |
22144 KB |
Output is correct |
46 |
Correct |
20 ms |
22400 KB |
Output is correct |
47 |
Correct |
18 ms |
22400 KB |
Output is correct |
48 |
Correct |
18 ms |
22272 KB |
Output is correct |
49 |
Correct |
18 ms |
22400 KB |
Output is correct |
50 |
Correct |
17 ms |
22272 KB |
Output is correct |
51 |
Correct |
17 ms |
22144 KB |
Output is correct |
52 |
Correct |
17 ms |
22272 KB |
Output is correct |
53 |
Correct |
18 ms |
22272 KB |
Output is correct |
54 |
Correct |
18 ms |
22400 KB |
Output is correct |
55 |
Correct |
24 ms |
22912 KB |
Output is correct |
56 |
Correct |
59 ms |
27384 KB |
Output is correct |
57 |
Correct |
96 ms |
31864 KB |
Output is correct |
58 |
Correct |
138 ms |
35064 KB |
Output is correct |
59 |
Correct |
183 ms |
39416 KB |
Output is correct |
60 |
Correct |
226 ms |
44024 KB |
Output is correct |
61 |
Correct |
270 ms |
47096 KB |
Output is correct |
62 |
Correct |
281 ms |
50296 KB |
Output is correct |
63 |
Correct |
339 ms |
56696 KB |
Output is correct |
64 |
Correct |
348 ms |
56952 KB |
Output is correct |
65 |
Correct |
175 ms |
69752 KB |
Output is correct |
66 |
Correct |
179 ms |
69752 KB |
Output is correct |
67 |
Correct |
173 ms |
37240 KB |
Output is correct |
68 |
Correct |
282 ms |
69112 KB |
Output is correct |
69 |
Correct |
343 ms |
67448 KB |
Output is correct |
70 |
Correct |
337 ms |
67540 KB |
Output is correct |
71 |
Correct |
486 ms |
98192 KB |
Output is correct |
72 |
Correct |
526 ms |
97784 KB |
Output is correct |
73 |
Correct |
581 ms |
84732 KB |
Output is correct |
74 |
Correct |
536 ms |
84856 KB |
Output is correct |
75 |
Correct |
516 ms |
84020 KB |
Output is correct |
76 |
Correct |
500 ms |
84216 KB |
Output is correct |
77 |
Correct |
497 ms |
82296 KB |
Output is correct |
78 |
Correct |
508 ms |
79348 KB |
Output is correct |
79 |
Correct |
504 ms |
83672 KB |
Output is correct |