#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=3e5;
int p[N+10];
int c[N+10];
int leaves[N+10];
int mx_son[N+10];
vector<pair<int,int>> e[N+10];
void count_leaves(int x)
{
if(e[x].empty())
{
leaves[x]=1;
return;
}
leaves[x]=0;
mx_son[x]=0;
for(auto v:e[x])
{
count_leaves(v.fi);
if(leaves[v.fi]>leaves[mx_son[x]])
mx_son[x]=v.fi;
leaves[x]+=leaves[v.fi];
}
return;
}
void add_edge(multiset<long long> &st,int cc)
{
if(st.empty())
{
st.insert(cc);
st.insert(cc);
return;
}
long long init_val[2];
for(int i:{0,1})
{
init_val[i]=*prev(st.end());
st.erase(prev(st.end()));
}
for(int i:{0,1})
st.insert(init_val[i]+cc);
return;
}
void solve(int x,multiset<long long> &st)
{
multiset<long long> tmp;
for(auto v:e[x])
{
if(v.fi==mx_son[x])
{
solve(v.fi,st);
add_edge(st,v.se);
break;
}
}
for(auto v:e[x])
{
if(v.fi==mx_son[x])
continue;
tmp.clear();
solve(v.fi,tmp);
add_edge(tmp,v.se);
for(auto vt:tmp)
st.insert(vt);
}
while(st.size()>leaves[x]+1)
st.erase(prev(st.end()));
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
long long ans=0;
cin>>n>>m;
for(int i=2;i<=n+m;i++)
{
cin>>p[i]>>c[i];
e[p[i]].push_back({i,c[i]});
ans+=c[i];
}
count_leaves(1);
multiset<long long> ans_set;
solve(1,ans_set);
long long w=-m,prv=0;
for(auto v:ans_set)
{
ans+=w*(v-prv);
prv=v;
w++;
}
cout<<ans<<"\n";
return 0;
}
Compilation message
fireworks.cpp: In function 'void solve(int, std::multiset<long long int>&)':
fireworks.cpp:69:17: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
69 | while(st.size()>leaves[x]+1)
| ~~~~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7424 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
7 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7404 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
6 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7404 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
6 ms |
7404 KB |
Output is correct |
19 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7424 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7424 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
6 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7404 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
6 ms |
7404 KB |
Output is correct |
19 |
Correct |
7 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7404 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
22 |
Correct |
6 ms |
7404 KB |
Output is correct |
23 |
Correct |
6 ms |
7404 KB |
Output is correct |
24 |
Correct |
6 ms |
7404 KB |
Output is correct |
25 |
Correct |
6 ms |
7404 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
6 ms |
7404 KB |
Output is correct |
28 |
Correct |
6 ms |
7404 KB |
Output is correct |
29 |
Correct |
6 ms |
7404 KB |
Output is correct |
30 |
Correct |
6 ms |
7404 KB |
Output is correct |
31 |
Correct |
7 ms |
7404 KB |
Output is correct |
32 |
Correct |
7 ms |
7532 KB |
Output is correct |
33 |
Correct |
7 ms |
7532 KB |
Output is correct |
34 |
Correct |
8 ms |
7532 KB |
Output is correct |
35 |
Correct |
8 ms |
7660 KB |
Output is correct |
36 |
Correct |
8 ms |
7660 KB |
Output is correct |
37 |
Correct |
9 ms |
7680 KB |
Output is correct |
38 |
Correct |
10 ms |
7660 KB |
Output is correct |
39 |
Correct |
9 ms |
7660 KB |
Output is correct |
40 |
Correct |
8 ms |
8556 KB |
Output is correct |
41 |
Correct |
9 ms |
8556 KB |
Output is correct |
42 |
Correct |
8 ms |
7660 KB |
Output is correct |
43 |
Correct |
9 ms |
8172 KB |
Output is correct |
44 |
Correct |
9 ms |
8044 KB |
Output is correct |
45 |
Correct |
9 ms |
8044 KB |
Output is correct |
46 |
Correct |
10 ms |
7788 KB |
Output is correct |
47 |
Correct |
10 ms |
7788 KB |
Output is correct |
48 |
Correct |
9 ms |
7788 KB |
Output is correct |
49 |
Correct |
10 ms |
7788 KB |
Output is correct |
50 |
Correct |
9 ms |
7788 KB |
Output is correct |
51 |
Correct |
9 ms |
7788 KB |
Output is correct |
52 |
Correct |
9 ms |
7788 KB |
Output is correct |
53 |
Correct |
9 ms |
7788 KB |
Output is correct |
54 |
Correct |
10 ms |
8044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7424 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7424 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
6 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7404 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
6 ms |
7404 KB |
Output is correct |
19 |
Correct |
7 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7404 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
22 |
Correct |
6 ms |
7404 KB |
Output is correct |
23 |
Correct |
6 ms |
7404 KB |
Output is correct |
24 |
Correct |
6 ms |
7404 KB |
Output is correct |
25 |
Correct |
6 ms |
7404 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
6 ms |
7404 KB |
Output is correct |
28 |
Correct |
6 ms |
7404 KB |
Output is correct |
29 |
Correct |
6 ms |
7404 KB |
Output is correct |
30 |
Correct |
6 ms |
7404 KB |
Output is correct |
31 |
Correct |
7 ms |
7404 KB |
Output is correct |
32 |
Correct |
7 ms |
7532 KB |
Output is correct |
33 |
Correct |
7 ms |
7532 KB |
Output is correct |
34 |
Correct |
8 ms |
7532 KB |
Output is correct |
35 |
Correct |
8 ms |
7660 KB |
Output is correct |
36 |
Correct |
8 ms |
7660 KB |
Output is correct |
37 |
Correct |
9 ms |
7680 KB |
Output is correct |
38 |
Correct |
10 ms |
7660 KB |
Output is correct |
39 |
Correct |
9 ms |
7660 KB |
Output is correct |
40 |
Correct |
8 ms |
8556 KB |
Output is correct |
41 |
Correct |
9 ms |
8556 KB |
Output is correct |
42 |
Correct |
8 ms |
7660 KB |
Output is correct |
43 |
Correct |
9 ms |
8172 KB |
Output is correct |
44 |
Correct |
9 ms |
8044 KB |
Output is correct |
45 |
Correct |
9 ms |
8044 KB |
Output is correct |
46 |
Correct |
10 ms |
7788 KB |
Output is correct |
47 |
Correct |
10 ms |
7788 KB |
Output is correct |
48 |
Correct |
9 ms |
7788 KB |
Output is correct |
49 |
Correct |
10 ms |
7788 KB |
Output is correct |
50 |
Correct |
9 ms |
7788 KB |
Output is correct |
51 |
Correct |
9 ms |
7788 KB |
Output is correct |
52 |
Correct |
9 ms |
7788 KB |
Output is correct |
53 |
Correct |
9 ms |
7788 KB |
Output is correct |
54 |
Correct |
10 ms |
8044 KB |
Output is correct |
55 |
Correct |
15 ms |
8428 KB |
Output is correct |
56 |
Correct |
44 ms |
10604 KB |
Output is correct |
57 |
Correct |
84 ms |
12524 KB |
Output is correct |
58 |
Correct |
103 ms |
14060 KB |
Output is correct |
59 |
Correct |
139 ms |
16364 KB |
Output is correct |
60 |
Correct |
189 ms |
17900 KB |
Output is correct |
61 |
Correct |
215 ms |
19692 KB |
Output is correct |
62 |
Correct |
252 ms |
21228 KB |
Output is correct |
63 |
Correct |
321 ms |
23444 KB |
Output is correct |
64 |
Correct |
318 ms |
23400 KB |
Output is correct |
65 |
Correct |
167 ms |
73452 KB |
Output is correct |
66 |
Correct |
172 ms |
73452 KB |
Output is correct |
67 |
Correct |
176 ms |
21868 KB |
Output is correct |
68 |
Correct |
233 ms |
49376 KB |
Output is correct |
69 |
Correct |
275 ms |
45932 KB |
Output is correct |
70 |
Correct |
279 ms |
45804 KB |
Output is correct |
71 |
Correct |
351 ms |
29036 KB |
Output is correct |
72 |
Correct |
353 ms |
29164 KB |
Output is correct |
73 |
Correct |
384 ms |
28908 KB |
Output is correct |
74 |
Correct |
377 ms |
28732 KB |
Output is correct |
75 |
Correct |
380 ms |
28396 KB |
Output is correct |
76 |
Correct |
386 ms |
28376 KB |
Output is correct |
77 |
Correct |
406 ms |
28008 KB |
Output is correct |
78 |
Correct |
460 ms |
28908 KB |
Output is correct |
79 |
Correct |
455 ms |
42076 KB |
Output is correct |