#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=6e5;
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;
}
int 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);
int w=-m,prv=0;
for(auto v:ans_set)
{
ans+=(long long)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)
| ~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14572 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
12 ms |
14572 KB |
Output is correct |
7 |
Correct |
12 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
12 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
11 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14572 KB |
Output is correct |
15 |
Correct |
11 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14444 KB |
Output is correct |
17 |
Correct |
11 ms |
14444 KB |
Output is correct |
18 |
Correct |
11 ms |
14444 KB |
Output is correct |
19 |
Correct |
11 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
11 ms |
14572 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14444 KB |
Output is correct |
15 |
Correct |
11 ms |
14444 KB |
Output is correct |
16 |
Correct |
12 ms |
14572 KB |
Output is correct |
17 |
Correct |
12 ms |
14444 KB |
Output is correct |
18 |
Correct |
12 ms |
14444 KB |
Output is correct |
19 |
Correct |
12 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
11 ms |
14444 KB |
Output is correct |
24 |
Correct |
11 ms |
14572 KB |
Output is correct |
25 |
Correct |
11 ms |
14444 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
11 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14444 KB |
Output is correct |
29 |
Correct |
11 ms |
14444 KB |
Output is correct |
30 |
Incorrect |
11 ms |
14444 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
11 ms |
14572 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14444 KB |
Output is correct |
15 |
Correct |
11 ms |
14444 KB |
Output is correct |
16 |
Correct |
12 ms |
14572 KB |
Output is correct |
17 |
Correct |
12 ms |
14444 KB |
Output is correct |
18 |
Correct |
12 ms |
14444 KB |
Output is correct |
19 |
Correct |
12 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
11 ms |
14444 KB |
Output is correct |
24 |
Correct |
11 ms |
14572 KB |
Output is correct |
25 |
Correct |
11 ms |
14444 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
11 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14444 KB |
Output is correct |
29 |
Correct |
11 ms |
14444 KB |
Output is correct |
30 |
Incorrect |
11 ms |
14444 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |