Submission #386055

# Submission time Handle Problem Language Result Execution time Memory
386055 2021-04-05T14:04:30 Z Jasiekstrz Fireworks (APIO16_fireworks) C++17
26 / 100
15 ms 15596 KB
#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;
	}
	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);
	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)
      |        ~~~~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 12 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 10 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
# Verdict Execution time Memory 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 10 ms 14444 KB Output is correct
5 Correct 10 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 10 ms 14444 KB Output is correct
9 Correct 11 ms 14444 KB Output is correct
10 Correct 12 ms 14444 KB Output is correct
11 Correct 11 ms 14464 KB Output is correct
12 Correct 10 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 10 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
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 12 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 10 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 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 10 ms 14444 KB Output is correct
15 Correct 10 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 10 ms 14444 KB Output is correct
19 Correct 11 ms 14444 KB Output is correct
20 Correct 12 ms 14444 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 10 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 10 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 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14572 KB Output is correct
32 Correct 12 ms 14572 KB Output is correct
33 Correct 14 ms 14572 KB Output is correct
34 Correct 13 ms 14700 KB Output is correct
35 Correct 15 ms 14700 KB Output is correct
36 Correct 13 ms 14700 KB Output is correct
37 Correct 14 ms 14828 KB Output is correct
38 Correct 14 ms 14828 KB Output is correct
39 Correct 14 ms 14828 KB Output is correct
40 Correct 13 ms 15596 KB Output is correct
41 Correct 13 ms 15596 KB Output is correct
42 Incorrect 13 ms 14828 KB Output isn't correct
43 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 12 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 10 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 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 10 ms 14444 KB Output is correct
15 Correct 10 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 10 ms 14444 KB Output is correct
19 Correct 11 ms 14444 KB Output is correct
20 Correct 12 ms 14444 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 10 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 10 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 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14572 KB Output is correct
32 Correct 12 ms 14572 KB Output is correct
33 Correct 14 ms 14572 KB Output is correct
34 Correct 13 ms 14700 KB Output is correct
35 Correct 15 ms 14700 KB Output is correct
36 Correct 13 ms 14700 KB Output is correct
37 Correct 14 ms 14828 KB Output is correct
38 Correct 14 ms 14828 KB Output is correct
39 Correct 14 ms 14828 KB Output is correct
40 Correct 13 ms 15596 KB Output is correct
41 Correct 13 ms 15596 KB Output is correct
42 Incorrect 13 ms 14828 KB Output isn't correct
43 Halted 0 ms 0 KB -