#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
vector<pair<llo,llo>> adj[300001];
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ord tree<pair<llo,llo>,null_type,less<pair<llo,llo>>,rb_tree_tag,tree_order_statistics_node_update>
ord xx[300001];
pair<llo,llo> fun[300001];
llo n,m;
llo cot=0;
llo dfs(llo no){
vector<llo> cur;
for(auto j:adj[no]){
if(j.a>=n){
fun[j.a]={-1,j.b};
xx[j.a].insert({j.b,cot});
cot++;
xx[j.a].insert({j.b,cot});
cot++;
cur.pb(j.a);
}
else{
llo x=dfs(j.a);
cur.pb(x);
llo la2=-1;
while(xx[x].size()+fun[x].a>0){
if(xx[x].size()+fun[x].a==1){
la2=(*(xx[x].find_by_order(xx[x].size()-1))).a;
}
xx[x].erase(xx[x].find_by_order(xx[x].size()-1));
}
llo la=(*(xx[x].find_by_order(xx[x].size()-1))).a;
if(la2==-1){
la2=la;
}
fun[x].b+=j.b;
if(xx[x].size()+fun[x].a==0){
xx[x].erase(xx[x].find_by_order(xx[x].size()-1));
}
else{
while(xx[x].size()+fun[x].a<-1){
xx[x].insert({la,cot});
cot++;
}
}
assert(xx[x].size()+fun[x].a==-1);
//sum is -1
//xx[x].insert({la,cot});
cot++;
xx[x].insert({la+j.b,cot});
cot++;
xx[x].insert({la2+j.b,cot});
cot++;
/*if(no==1){
cout<<fun[x].a<<":"<<fun[x].b<<endl;
for(auto j:xx[x]){
cout<<j.a<<",";
}
cout<<endl;
}*/
//fun[x].a+=-1;
//fun[x].b+=j.b;
}
}
pair<llo,llo> ma={-1,-1};
for(auto j:cur){
ma=max(ma,{(llo)xx[j].size(),j});
}
for(auto j:cur){
if(j!=ma.b){
for(auto jj:xx[j]){
xx[ma.b].insert(jj);
}
fun[ma.b].a+=fun[j].a;
fun[ma.b].b+=fun[j].b;
}
}
/*cout<<no<<"::"<<ma.b<<endl;
cout<<fun[ma.b].a<<";"<<fun[ma.b].b<<endl;
for(auto j:xx[ma.b]){
cout<<j.a<<",";
}
cout<<endl;*/
return ma.b;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(llo i=1;i<n+m;i++){
llo aa,bb;
cin>>aa>>bb;
aa--;
adj[aa].pb({i,bb});
}
llo ha=dfs(0);
llo ans=fun[ha].a;
vector<llo> ne;
for(auto j:xx[ha]){
ne.pb(j.a);
}
llo val=fun[ha].b;
llo pr=0;
for(llo i=0;i<ne.size();i++){
if(fun[ha].a>=0){
break;
}
val+=fun[ha].a*(ne[i]-pr);
pr=ne[i];
fun[ha].a+=1;
}
cout<<val<<endl;
/*cout<<fun[ha].a<<"::"<<fun[ha].b<<endl;
for(auto j:xx[ha]){
cout<<j.a<<",";
}
cout<<endl;
*/
return 0;
}
Compilation message
fireworks.cpp: In function 'llo dfs(llo)':
fireworks.cpp:52:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
52 | while(xx[x].size()+fun[x].a<-1){
| ~~~~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/c++/10/ext/pb_ds/detail/pat_trie_/pat_trie_.hpp:45,
from /usr/include/c++/10/ext/pb_ds/detail/container_base_dispatch.hpp:90,
from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:48,
from fireworks.cpp:12:
fireworks.cpp:57:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
57 | assert(xx[x].size()+fun[x].a==-1);
| ~~~~~~~~~~~~~~~~~~~~~^~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:126:15: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for(llo i=0;i<ne.size();i++){
| ~^~~~~~~~~~
fireworks.cpp:119:6: warning: unused variable 'ans' [-Wunused-variable]
119 | llo ans=fun[ha].a;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35932 KB |
Output is correct |
2 |
Correct |
16 ms |
35924 KB |
Output is correct |
3 |
Correct |
15 ms |
35928 KB |
Output is correct |
4 |
Correct |
15 ms |
35932 KB |
Output is correct |
5 |
Correct |
14 ms |
36064 KB |
Output is correct |
6 |
Correct |
14 ms |
35932 KB |
Output is correct |
7 |
Correct |
14 ms |
35892 KB |
Output is correct |
8 |
Correct |
14 ms |
35948 KB |
Output is correct |
9 |
Correct |
14 ms |
35928 KB |
Output is correct |
10 |
Correct |
15 ms |
35932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
35920 KB |
Output is correct |
2 |
Correct |
14 ms |
35932 KB |
Output is correct |
3 |
Correct |
14 ms |
35920 KB |
Output is correct |
4 |
Correct |
15 ms |
35928 KB |
Output is correct |
5 |
Correct |
17 ms |
35932 KB |
Output is correct |
6 |
Correct |
17 ms |
35976 KB |
Output is correct |
7 |
Correct |
18 ms |
35912 KB |
Output is correct |
8 |
Correct |
17 ms |
35932 KB |
Output is correct |
9 |
Correct |
16 ms |
36024 KB |
Output is correct |
10 |
Correct |
22 ms |
35932 KB |
Output is correct |
11 |
Correct |
20 ms |
36188 KB |
Output is correct |
12 |
Correct |
16 ms |
35932 KB |
Output is correct |
13 |
Correct |
14 ms |
35932 KB |
Output is correct |
14 |
Correct |
15 ms |
36188 KB |
Output is correct |
15 |
Correct |
18 ms |
35928 KB |
Output is correct |
16 |
Correct |
15 ms |
35932 KB |
Output is correct |
17 |
Correct |
15 ms |
35960 KB |
Output is correct |
18 |
Correct |
15 ms |
35932 KB |
Output is correct |
19 |
Correct |
15 ms |
35932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35932 KB |
Output is correct |
2 |
Correct |
16 ms |
35924 KB |
Output is correct |
3 |
Correct |
15 ms |
35928 KB |
Output is correct |
4 |
Correct |
15 ms |
35932 KB |
Output is correct |
5 |
Correct |
14 ms |
36064 KB |
Output is correct |
6 |
Correct |
14 ms |
35932 KB |
Output is correct |
7 |
Correct |
14 ms |
35892 KB |
Output is correct |
8 |
Correct |
14 ms |
35948 KB |
Output is correct |
9 |
Correct |
14 ms |
35928 KB |
Output is correct |
10 |
Correct |
15 ms |
35932 KB |
Output is correct |
11 |
Correct |
14 ms |
35920 KB |
Output is correct |
12 |
Correct |
14 ms |
35932 KB |
Output is correct |
13 |
Correct |
14 ms |
35920 KB |
Output is correct |
14 |
Correct |
15 ms |
35928 KB |
Output is correct |
15 |
Correct |
17 ms |
35932 KB |
Output is correct |
16 |
Correct |
17 ms |
35976 KB |
Output is correct |
17 |
Correct |
18 ms |
35912 KB |
Output is correct |
18 |
Correct |
17 ms |
35932 KB |
Output is correct |
19 |
Correct |
16 ms |
36024 KB |
Output is correct |
20 |
Correct |
22 ms |
35932 KB |
Output is correct |
21 |
Correct |
20 ms |
36188 KB |
Output is correct |
22 |
Correct |
16 ms |
35932 KB |
Output is correct |
23 |
Correct |
14 ms |
35932 KB |
Output is correct |
24 |
Correct |
15 ms |
36188 KB |
Output is correct |
25 |
Correct |
18 ms |
35928 KB |
Output is correct |
26 |
Correct |
15 ms |
35932 KB |
Output is correct |
27 |
Correct |
15 ms |
35960 KB |
Output is correct |
28 |
Correct |
15 ms |
35932 KB |
Output is correct |
29 |
Correct |
15 ms |
35932 KB |
Output is correct |
30 |
Correct |
20 ms |
36172 KB |
Output is correct |
31 |
Correct |
16 ms |
36188 KB |
Output is correct |
32 |
Correct |
18 ms |
36212 KB |
Output is correct |
33 |
Correct |
18 ms |
36444 KB |
Output is correct |
34 |
Correct |
18 ms |
36444 KB |
Output is correct |
35 |
Correct |
17 ms |
36700 KB |
Output is correct |
36 |
Correct |
19 ms |
36700 KB |
Output is correct |
37 |
Correct |
20 ms |
36700 KB |
Output is correct |
38 |
Correct |
19 ms |
36956 KB |
Output is correct |
39 |
Correct |
21 ms |
36956 KB |
Output is correct |
40 |
Correct |
19 ms |
37212 KB |
Output is correct |
41 |
Correct |
19 ms |
37212 KB |
Output is correct |
42 |
Correct |
19 ms |
36188 KB |
Output is correct |
43 |
Correct |
20 ms |
37248 KB |
Output is correct |
44 |
Correct |
19 ms |
37100 KB |
Output is correct |
45 |
Correct |
20 ms |
37028 KB |
Output is correct |
46 |
Correct |
21 ms |
37468 KB |
Output is correct |
47 |
Correct |
21 ms |
37468 KB |
Output is correct |
48 |
Correct |
22 ms |
37468 KB |
Output is correct |
49 |
Correct |
21 ms |
37500 KB |
Output is correct |
50 |
Correct |
22 ms |
37212 KB |
Output is correct |
51 |
Correct |
21 ms |
37212 KB |
Output is correct |
52 |
Correct |
26 ms |
37204 KB |
Output is correct |
53 |
Correct |
23 ms |
37256 KB |
Output is correct |
54 |
Correct |
21 ms |
37736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
35932 KB |
Output is correct |
2 |
Correct |
16 ms |
35924 KB |
Output is correct |
3 |
Correct |
15 ms |
35928 KB |
Output is correct |
4 |
Correct |
15 ms |
35932 KB |
Output is correct |
5 |
Correct |
14 ms |
36064 KB |
Output is correct |
6 |
Correct |
14 ms |
35932 KB |
Output is correct |
7 |
Correct |
14 ms |
35892 KB |
Output is correct |
8 |
Correct |
14 ms |
35948 KB |
Output is correct |
9 |
Correct |
14 ms |
35928 KB |
Output is correct |
10 |
Correct |
15 ms |
35932 KB |
Output is correct |
11 |
Correct |
14 ms |
35920 KB |
Output is correct |
12 |
Correct |
14 ms |
35932 KB |
Output is correct |
13 |
Correct |
14 ms |
35920 KB |
Output is correct |
14 |
Correct |
15 ms |
35928 KB |
Output is correct |
15 |
Correct |
17 ms |
35932 KB |
Output is correct |
16 |
Correct |
17 ms |
35976 KB |
Output is correct |
17 |
Correct |
18 ms |
35912 KB |
Output is correct |
18 |
Correct |
17 ms |
35932 KB |
Output is correct |
19 |
Correct |
16 ms |
36024 KB |
Output is correct |
20 |
Correct |
22 ms |
35932 KB |
Output is correct |
21 |
Correct |
20 ms |
36188 KB |
Output is correct |
22 |
Correct |
16 ms |
35932 KB |
Output is correct |
23 |
Correct |
14 ms |
35932 KB |
Output is correct |
24 |
Correct |
15 ms |
36188 KB |
Output is correct |
25 |
Correct |
18 ms |
35928 KB |
Output is correct |
26 |
Correct |
15 ms |
35932 KB |
Output is correct |
27 |
Correct |
15 ms |
35960 KB |
Output is correct |
28 |
Correct |
15 ms |
35932 KB |
Output is correct |
29 |
Correct |
15 ms |
35932 KB |
Output is correct |
30 |
Correct |
20 ms |
36172 KB |
Output is correct |
31 |
Correct |
16 ms |
36188 KB |
Output is correct |
32 |
Correct |
18 ms |
36212 KB |
Output is correct |
33 |
Correct |
18 ms |
36444 KB |
Output is correct |
34 |
Correct |
18 ms |
36444 KB |
Output is correct |
35 |
Correct |
17 ms |
36700 KB |
Output is correct |
36 |
Correct |
19 ms |
36700 KB |
Output is correct |
37 |
Correct |
20 ms |
36700 KB |
Output is correct |
38 |
Correct |
19 ms |
36956 KB |
Output is correct |
39 |
Correct |
21 ms |
36956 KB |
Output is correct |
40 |
Correct |
19 ms |
37212 KB |
Output is correct |
41 |
Correct |
19 ms |
37212 KB |
Output is correct |
42 |
Correct |
19 ms |
36188 KB |
Output is correct |
43 |
Correct |
20 ms |
37248 KB |
Output is correct |
44 |
Correct |
19 ms |
37100 KB |
Output is correct |
45 |
Correct |
20 ms |
37028 KB |
Output is correct |
46 |
Correct |
21 ms |
37468 KB |
Output is correct |
47 |
Correct |
21 ms |
37468 KB |
Output is correct |
48 |
Correct |
22 ms |
37468 KB |
Output is correct |
49 |
Correct |
21 ms |
37500 KB |
Output is correct |
50 |
Correct |
22 ms |
37212 KB |
Output is correct |
51 |
Correct |
21 ms |
37212 KB |
Output is correct |
52 |
Correct |
26 ms |
37204 KB |
Output is correct |
53 |
Correct |
23 ms |
37256 KB |
Output is correct |
54 |
Correct |
21 ms |
37736 KB |
Output is correct |
55 |
Correct |
26 ms |
40284 KB |
Output is correct |
56 |
Correct |
52 ms |
46724 KB |
Output is correct |
57 |
Correct |
82 ms |
53204 KB |
Output is correct |
58 |
Correct |
108 ms |
57992 KB |
Output is correct |
59 |
Correct |
128 ms |
66256 KB |
Output is correct |
60 |
Correct |
164 ms |
72708 KB |
Output is correct |
61 |
Correct |
209 ms |
77728 KB |
Output is correct |
62 |
Correct |
213 ms |
82124 KB |
Output is correct |
63 |
Correct |
256 ms |
89036 KB |
Output is correct |
64 |
Correct |
269 ms |
89812 KB |
Output is correct |
65 |
Correct |
123 ms |
111188 KB |
Output is correct |
66 |
Correct |
120 ms |
111188 KB |
Output is correct |
67 |
Correct |
97 ms |
50516 KB |
Output is correct |
68 |
Correct |
263 ms |
110792 KB |
Output is correct |
69 |
Correct |
296 ms |
106960 KB |
Output is correct |
70 |
Correct |
299 ms |
106960 KB |
Output is correct |
71 |
Correct |
405 ms |
147652 KB |
Output is correct |
72 |
Correct |
422 ms |
147796 KB |
Output is correct |
73 |
Correct |
383 ms |
129488 KB |
Output is correct |
74 |
Correct |
407 ms |
130468 KB |
Output is correct |
75 |
Correct |
381 ms |
129388 KB |
Output is correct |
76 |
Correct |
387 ms |
130328 KB |
Output is correct |
77 |
Correct |
368 ms |
126692 KB |
Output is correct |
78 |
Correct |
386 ms |
124228 KB |
Output is correct |
79 |
Correct |
411 ms |
139008 KB |
Output is correct |