#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 30, MOD = 1e9 + 7;
int n,m;
vector<pair<int,int>> g[N];
multiset<ll> st[N];
ll h[N],l[N],r[N];
int mx[N];
void merge(int v,int to){
if((int)st[v].size() < (int)st[to].size()){
st[v].swap(st[to]);
swap(mx[v],mx[to]);
swap(l[v],l[to]);
swap(r[v],r[to]);
}
mx[v]+=mx[to];
for(ll j:st[to]){
st[v].insert(j);
}
st[to].clear();
}
void dfs(int v,int W = 0){
if(g[v].empty()){
st[v].insert(W);
st[v].insert(W);
mx[v] = 1;
h[v] = 0;
l[v] = r[v] = W;
return;
}
for(auto [to,w]:g[v]){
dfs(to,w);
merge(v,to);
}
assert(mx[v] == (int)g[v].size());
while(mx[v] > 1){
mx[v]--;
st[v].erase(st[v].find(*st[v].rbegin()));
}
ll x = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
ll y = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
st[v].insert(x + W);
st[v].insert(y + W);
r[v] = (*st[v].rbegin());
l[v] = (*(++(st[v].rbegin())));
}
ll E = 0;
void test(){
cin >> n >> m;
for(int i = 2;i <= n + m;i++){
int a,b;
cin >> a >> b;
g[a].push_back({i,b});
E += b;
}
dfs(1);
st[1].erase(--st[1].end());
ll H = 0;
for(ll i:st[1]){
E -= i;
}
cout << E << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
Compilation message
fireworks.cpp: In function 'void test()':
fireworks.cpp:64:8: warning: unused variable 'H' [-Wunused-variable]
64 | ll H = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
35160 KB |
Output is correct |
2 |
Correct |
5 ms |
35164 KB |
Output is correct |
3 |
Correct |
6 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35164 KB |
Output is correct |
5 |
Correct |
7 ms |
35164 KB |
Output is correct |
6 |
Correct |
5 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
5 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35164 KB |
Output is correct |
10 |
Correct |
7 ms |
35164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35164 KB |
Output is correct |
2 |
Correct |
5 ms |
35164 KB |
Output is correct |
3 |
Correct |
5 ms |
35284 KB |
Output is correct |
4 |
Correct |
5 ms |
35164 KB |
Output is correct |
5 |
Correct |
6 ms |
35164 KB |
Output is correct |
6 |
Correct |
7 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35256 KB |
Output is correct |
8 |
Correct |
6 ms |
35164 KB |
Output is correct |
9 |
Correct |
7 ms |
35164 KB |
Output is correct |
10 |
Correct |
5 ms |
35272 KB |
Output is correct |
11 |
Correct |
5 ms |
35164 KB |
Output is correct |
12 |
Correct |
6 ms |
35164 KB |
Output is correct |
13 |
Correct |
6 ms |
35164 KB |
Output is correct |
14 |
Correct |
5 ms |
35164 KB |
Output is correct |
15 |
Correct |
5 ms |
35164 KB |
Output is correct |
16 |
Correct |
5 ms |
35164 KB |
Output is correct |
17 |
Correct |
5 ms |
35160 KB |
Output is correct |
18 |
Correct |
5 ms |
35160 KB |
Output is correct |
19 |
Correct |
5 ms |
35160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
35160 KB |
Output is correct |
2 |
Correct |
5 ms |
35164 KB |
Output is correct |
3 |
Correct |
6 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35164 KB |
Output is correct |
5 |
Correct |
7 ms |
35164 KB |
Output is correct |
6 |
Correct |
5 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
5 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35164 KB |
Output is correct |
10 |
Correct |
7 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
5 ms |
35164 KB |
Output is correct |
13 |
Correct |
5 ms |
35284 KB |
Output is correct |
14 |
Correct |
5 ms |
35164 KB |
Output is correct |
15 |
Correct |
6 ms |
35164 KB |
Output is correct |
16 |
Correct |
7 ms |
35164 KB |
Output is correct |
17 |
Correct |
5 ms |
35256 KB |
Output is correct |
18 |
Correct |
6 ms |
35164 KB |
Output is correct |
19 |
Correct |
7 ms |
35164 KB |
Output is correct |
20 |
Correct |
5 ms |
35272 KB |
Output is correct |
21 |
Correct |
5 ms |
35164 KB |
Output is correct |
22 |
Correct |
6 ms |
35164 KB |
Output is correct |
23 |
Correct |
6 ms |
35164 KB |
Output is correct |
24 |
Correct |
5 ms |
35164 KB |
Output is correct |
25 |
Correct |
5 ms |
35164 KB |
Output is correct |
26 |
Correct |
5 ms |
35164 KB |
Output is correct |
27 |
Correct |
5 ms |
35160 KB |
Output is correct |
28 |
Correct |
5 ms |
35160 KB |
Output is correct |
29 |
Correct |
5 ms |
35160 KB |
Output is correct |
30 |
Correct |
5 ms |
35164 KB |
Output is correct |
31 |
Correct |
6 ms |
35164 KB |
Output is correct |
32 |
Correct |
5 ms |
35160 KB |
Output is correct |
33 |
Correct |
5 ms |
35420 KB |
Output is correct |
34 |
Correct |
5 ms |
35672 KB |
Output is correct |
35 |
Correct |
7 ms |
35416 KB |
Output is correct |
36 |
Correct |
6 ms |
35416 KB |
Output is correct |
37 |
Correct |
7 ms |
35496 KB |
Output is correct |
38 |
Correct |
6 ms |
35420 KB |
Output is correct |
39 |
Correct |
7 ms |
35420 KB |
Output is correct |
40 |
Correct |
6 ms |
35932 KB |
Output is correct |
41 |
Correct |
6 ms |
35932 KB |
Output is correct |
42 |
Correct |
6 ms |
35456 KB |
Output is correct |
43 |
Correct |
6 ms |
35676 KB |
Output is correct |
44 |
Correct |
7 ms |
35672 KB |
Output is correct |
45 |
Correct |
6 ms |
35676 KB |
Output is correct |
46 |
Correct |
6 ms |
35576 KB |
Output is correct |
47 |
Correct |
7 ms |
35420 KB |
Output is correct |
48 |
Correct |
7 ms |
35420 KB |
Output is correct |
49 |
Correct |
7 ms |
35676 KB |
Output is correct |
50 |
Correct |
6 ms |
35676 KB |
Output is correct |
51 |
Correct |
6 ms |
35676 KB |
Output is correct |
52 |
Correct |
7 ms |
35420 KB |
Output is correct |
53 |
Correct |
7 ms |
35420 KB |
Output is correct |
54 |
Correct |
7 ms |
35672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
35160 KB |
Output is correct |
2 |
Correct |
5 ms |
35164 KB |
Output is correct |
3 |
Correct |
6 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35164 KB |
Output is correct |
5 |
Correct |
7 ms |
35164 KB |
Output is correct |
6 |
Correct |
5 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
5 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35164 KB |
Output is correct |
10 |
Correct |
7 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
5 ms |
35164 KB |
Output is correct |
13 |
Correct |
5 ms |
35284 KB |
Output is correct |
14 |
Correct |
5 ms |
35164 KB |
Output is correct |
15 |
Correct |
6 ms |
35164 KB |
Output is correct |
16 |
Correct |
7 ms |
35164 KB |
Output is correct |
17 |
Correct |
5 ms |
35256 KB |
Output is correct |
18 |
Correct |
6 ms |
35164 KB |
Output is correct |
19 |
Correct |
7 ms |
35164 KB |
Output is correct |
20 |
Correct |
5 ms |
35272 KB |
Output is correct |
21 |
Correct |
5 ms |
35164 KB |
Output is correct |
22 |
Correct |
6 ms |
35164 KB |
Output is correct |
23 |
Correct |
6 ms |
35164 KB |
Output is correct |
24 |
Correct |
5 ms |
35164 KB |
Output is correct |
25 |
Correct |
5 ms |
35164 KB |
Output is correct |
26 |
Correct |
5 ms |
35164 KB |
Output is correct |
27 |
Correct |
5 ms |
35160 KB |
Output is correct |
28 |
Correct |
5 ms |
35160 KB |
Output is correct |
29 |
Correct |
5 ms |
35160 KB |
Output is correct |
30 |
Correct |
5 ms |
35164 KB |
Output is correct |
31 |
Correct |
6 ms |
35164 KB |
Output is correct |
32 |
Correct |
5 ms |
35160 KB |
Output is correct |
33 |
Correct |
5 ms |
35420 KB |
Output is correct |
34 |
Correct |
5 ms |
35672 KB |
Output is correct |
35 |
Correct |
7 ms |
35416 KB |
Output is correct |
36 |
Correct |
6 ms |
35416 KB |
Output is correct |
37 |
Correct |
7 ms |
35496 KB |
Output is correct |
38 |
Correct |
6 ms |
35420 KB |
Output is correct |
39 |
Correct |
7 ms |
35420 KB |
Output is correct |
40 |
Correct |
6 ms |
35932 KB |
Output is correct |
41 |
Correct |
6 ms |
35932 KB |
Output is correct |
42 |
Correct |
6 ms |
35456 KB |
Output is correct |
43 |
Correct |
6 ms |
35676 KB |
Output is correct |
44 |
Correct |
7 ms |
35672 KB |
Output is correct |
45 |
Correct |
6 ms |
35676 KB |
Output is correct |
46 |
Correct |
6 ms |
35576 KB |
Output is correct |
47 |
Correct |
7 ms |
35420 KB |
Output is correct |
48 |
Correct |
7 ms |
35420 KB |
Output is correct |
49 |
Correct |
7 ms |
35676 KB |
Output is correct |
50 |
Correct |
6 ms |
35676 KB |
Output is correct |
51 |
Correct |
6 ms |
35676 KB |
Output is correct |
52 |
Correct |
7 ms |
35420 KB |
Output is correct |
53 |
Correct |
7 ms |
35420 KB |
Output is correct |
54 |
Correct |
7 ms |
35672 KB |
Output is correct |
55 |
Correct |
11 ms |
35928 KB |
Output is correct |
56 |
Correct |
25 ms |
37464 KB |
Output is correct |
57 |
Correct |
42 ms |
40908 KB |
Output is correct |
58 |
Correct |
56 ms |
44116 KB |
Output is correct |
59 |
Correct |
85 ms |
43604 KB |
Output is correct |
60 |
Correct |
122 ms |
47440 KB |
Output is correct |
61 |
Correct |
106 ms |
49236 KB |
Output is correct |
62 |
Correct |
124 ms |
53072 KB |
Output is correct |
63 |
Correct |
211 ms |
55232 KB |
Output is correct |
64 |
Correct |
193 ms |
55124 KB |
Output is correct |
65 |
Correct |
90 ms |
81488 KB |
Output is correct |
66 |
Correct |
97 ms |
81488 KB |
Output is correct |
67 |
Correct |
94 ms |
53332 KB |
Output is correct |
68 |
Correct |
129 ms |
70092 KB |
Output is correct |
69 |
Correct |
161 ms |
68436 KB |
Output is correct |
70 |
Correct |
186 ms |
68436 KB |
Output is correct |
71 |
Correct |
203 ms |
61228 KB |
Output is correct |
72 |
Correct |
204 ms |
61400 KB |
Output is correct |
73 |
Correct |
265 ms |
60644 KB |
Output is correct |
74 |
Correct |
191 ms |
61008 KB |
Output is correct |
75 |
Correct |
198 ms |
60200 KB |
Output is correct |
76 |
Correct |
239 ms |
60272 KB |
Output is correct |
77 |
Correct |
230 ms |
60236 KB |
Output is correct |
78 |
Correct |
283 ms |
60872 KB |
Output is correct |
79 |
Correct |
285 ms |
73568 KB |
Output is correct |