Submission #933272

# Submission time Handle Problem Language Result Execution time Memory
933272 2024-02-25T11:09:56 Z amirhoseinfar1385 Fireworks (APIO16_fireworks) C++17
100 / 100
235 ms 85088 KB
#include<bits/stdc++.h>
using namespace std;
const long long maxn=300000+10;
vector<long long>adj[maxn];
pair<long long,long long>par[maxn];
long long n;
long long sum=0;

void vorod(){
	long long d;
	cin>>n>>d;
	n+=d;
	for(long long i=2;i<=n;i++){
		cin>>par[i].first>>par[i].second;
		adj[par[i].first].push_back(i);
		sum+=par[i].second;
	}
}
priority_queue<long long>pq[maxn];
priority_queue <long long,vector<long long>, greater<long long>>revpq[maxn];
long long val[maxn];
void add(long long u,long long x,long long z){
	long long ps=pq[u].top(),nx=revpq[u].top();
	if(z==0){
		if(x<=nx){
			pq[u].push(x);
		}
		else{
			pq[u].push(revpq[u].top());
			revpq[u].pop();
			revpq[u].push(x);
		}
	}
	else{
		if(x>=ps){
			revpq[u].push(x);
		}
		else{
			revpq[u].push(pq[u].top());
			pq[u].pop();
			pq[u].push(x);
		}
	}
}

void merge(long long u,long long v){
	long long fu=u;
	if(pq[u].size()+revpq[u].size()<pq[v].size()+revpq[u].size()){
		swap(u,v);
	}
	while(pq[v].size()>0){
		add(u,pq[v].top()+val[v]-val[u],0);
		pq[v].pop();
	}
	while(revpq[v].size()>0){
		add(u,revpq[v].top()+val[v]-val[u],1);
		revpq[v].pop();
	}
	if(u!=fu){
		swap(pq[u],pq[v]);
		swap(revpq[u],revpq[v]);
		swap(val[u],val[v]);
	}
}

void solve(long long u,long long len=0){
	if(u!=1){
		len+=par[u].second;
	}
	for(auto x:adj[u]){
		solve(x,len);
		merge(u,x);
	}
	if((long long)adj[u].size()==0){
		pq[u].push(len);
		revpq[u].push(len);
	}
	if(u!=1){
		val[u]-=par[u].second;
		long long hey=revpq[u].top();
		while(revpq[u].size()>0){
			revpq[u].pop();
		}
		revpq[u].push(hey+par[u].second);
		hey=pq[u].top();
		pq[u].pop();
		pq[u].push(hey+par[u].second);
	}
	return ;
}

void khor(){
	//cout<<"suma: "<<sum<<" "<<val[1]<<endl;
	vector<long long>all;
	while((long long)pq[1].size()>0){
		all.push_back(pq[1].top()+val[1]);
		//cout<<pq[1].top()+val[1]<<endl;
		pq[1].pop();
	}
	all.push_back(0);
	long long res=sum;
	long long sz=all.size();
	for(long long i=0;i<sz-1;i++){
		////cout<<"tof: "<<all[i]<<" "<<i<<" "<<(all[i]-all[i+1])*i<<endl;
		res-=(all[i]-all[i+1])*((i+1));
	}
	cout<<res<<endl;
}	

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	//cout.tie(0);
	vorod();
	solve(1);
	khor();
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29016 KB Output is correct
2 Correct 6 ms 29020 KB Output is correct
3 Correct 7 ms 29020 KB Output is correct
4 Correct 6 ms 29020 KB Output is correct
5 Correct 7 ms 29016 KB Output is correct
6 Correct 7 ms 29020 KB Output is correct
7 Correct 6 ms 29020 KB Output is correct
8 Correct 6 ms 29020 KB Output is correct
9 Correct 6 ms 29020 KB Output is correct
10 Correct 6 ms 29016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29020 KB Output is correct
2 Correct 7 ms 29020 KB Output is correct
3 Correct 6 ms 29008 KB Output is correct
4 Correct 6 ms 29020 KB Output is correct
5 Correct 7 ms 29016 KB Output is correct
6 Correct 6 ms 29020 KB Output is correct
7 Correct 7 ms 29020 KB Output is correct
8 Correct 7 ms 28948 KB Output is correct
9 Correct 6 ms 29020 KB Output is correct
10 Correct 7 ms 29020 KB Output is correct
11 Correct 7 ms 29020 KB Output is correct
12 Correct 6 ms 29020 KB Output is correct
13 Correct 6 ms 29020 KB Output is correct
14 Correct 6 ms 29020 KB Output is correct
15 Correct 6 ms 29020 KB Output is correct
16 Correct 7 ms 29020 KB Output is correct
17 Correct 7 ms 29020 KB Output is correct
18 Correct 6 ms 29020 KB Output is correct
19 Correct 7 ms 29020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29016 KB Output is correct
2 Correct 6 ms 29020 KB Output is correct
3 Correct 7 ms 29020 KB Output is correct
4 Correct 6 ms 29020 KB Output is correct
5 Correct 7 ms 29016 KB Output is correct
6 Correct 7 ms 29020 KB Output is correct
7 Correct 6 ms 29020 KB Output is correct
8 Correct 6 ms 29020 KB Output is correct
9 Correct 6 ms 29020 KB Output is correct
10 Correct 6 ms 29016 KB Output is correct
11 Correct 6 ms 29020 KB Output is correct
12 Correct 7 ms 29020 KB Output is correct
13 Correct 6 ms 29008 KB Output is correct
14 Correct 6 ms 29020 KB Output is correct
15 Correct 7 ms 29016 KB Output is correct
16 Correct 6 ms 29020 KB Output is correct
17 Correct 7 ms 29020 KB Output is correct
18 Correct 7 ms 28948 KB Output is correct
19 Correct 6 ms 29020 KB Output is correct
20 Correct 7 ms 29020 KB Output is correct
21 Correct 7 ms 29020 KB Output is correct
22 Correct 6 ms 29020 KB Output is correct
23 Correct 6 ms 29020 KB Output is correct
24 Correct 6 ms 29020 KB Output is correct
25 Correct 6 ms 29020 KB Output is correct
26 Correct 7 ms 29020 KB Output is correct
27 Correct 7 ms 29020 KB Output is correct
28 Correct 6 ms 29020 KB Output is correct
29 Correct 7 ms 29020 KB Output is correct
30 Correct 7 ms 29016 KB Output is correct
31 Correct 7 ms 29164 KB Output is correct
32 Correct 9 ms 29020 KB Output is correct
33 Correct 7 ms 29020 KB Output is correct
34 Correct 8 ms 29276 KB Output is correct
35 Correct 8 ms 29276 KB Output is correct
36 Correct 8 ms 29276 KB Output is correct
37 Correct 9 ms 29532 KB Output is correct
38 Correct 9 ms 29276 KB Output is correct
39 Correct 9 ms 29260 KB Output is correct
40 Correct 8 ms 29788 KB Output is correct
41 Correct 8 ms 29784 KB Output is correct
42 Correct 8 ms 29272 KB Output is correct
43 Correct 8 ms 29588 KB Output is correct
44 Correct 9 ms 29532 KB Output is correct
45 Correct 9 ms 29532 KB Output is correct
46 Correct 10 ms 29788 KB Output is correct
47 Correct 10 ms 29764 KB Output is correct
48 Correct 9 ms 29532 KB Output is correct
49 Correct 12 ms 29788 KB Output is correct
50 Correct 9 ms 29532 KB Output is correct
51 Correct 8 ms 29528 KB Output is correct
52 Correct 9 ms 29784 KB Output is correct
53 Correct 9 ms 29532 KB Output is correct
54 Correct 10 ms 29532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29016 KB Output is correct
2 Correct 6 ms 29020 KB Output is correct
3 Correct 7 ms 29020 KB Output is correct
4 Correct 6 ms 29020 KB Output is correct
5 Correct 7 ms 29016 KB Output is correct
6 Correct 7 ms 29020 KB Output is correct
7 Correct 6 ms 29020 KB Output is correct
8 Correct 6 ms 29020 KB Output is correct
9 Correct 6 ms 29020 KB Output is correct
10 Correct 6 ms 29016 KB Output is correct
11 Correct 6 ms 29020 KB Output is correct
12 Correct 7 ms 29020 KB Output is correct
13 Correct 6 ms 29008 KB Output is correct
14 Correct 6 ms 29020 KB Output is correct
15 Correct 7 ms 29016 KB Output is correct
16 Correct 6 ms 29020 KB Output is correct
17 Correct 7 ms 29020 KB Output is correct
18 Correct 7 ms 28948 KB Output is correct
19 Correct 6 ms 29020 KB Output is correct
20 Correct 7 ms 29020 KB Output is correct
21 Correct 7 ms 29020 KB Output is correct
22 Correct 6 ms 29020 KB Output is correct
23 Correct 6 ms 29020 KB Output is correct
24 Correct 6 ms 29020 KB Output is correct
25 Correct 6 ms 29020 KB Output is correct
26 Correct 7 ms 29020 KB Output is correct
27 Correct 7 ms 29020 KB Output is correct
28 Correct 6 ms 29020 KB Output is correct
29 Correct 7 ms 29020 KB Output is correct
30 Correct 7 ms 29016 KB Output is correct
31 Correct 7 ms 29164 KB Output is correct
32 Correct 9 ms 29020 KB Output is correct
33 Correct 7 ms 29020 KB Output is correct
34 Correct 8 ms 29276 KB Output is correct
35 Correct 8 ms 29276 KB Output is correct
36 Correct 8 ms 29276 KB Output is correct
37 Correct 9 ms 29532 KB Output is correct
38 Correct 9 ms 29276 KB Output is correct
39 Correct 9 ms 29260 KB Output is correct
40 Correct 8 ms 29788 KB Output is correct
41 Correct 8 ms 29784 KB Output is correct
42 Correct 8 ms 29272 KB Output is correct
43 Correct 8 ms 29588 KB Output is correct
44 Correct 9 ms 29532 KB Output is correct
45 Correct 9 ms 29532 KB Output is correct
46 Correct 10 ms 29788 KB Output is correct
47 Correct 10 ms 29764 KB Output is correct
48 Correct 9 ms 29532 KB Output is correct
49 Correct 12 ms 29788 KB Output is correct
50 Correct 9 ms 29532 KB Output is correct
51 Correct 8 ms 29528 KB Output is correct
52 Correct 9 ms 29784 KB Output is correct
53 Correct 9 ms 29532 KB Output is correct
54 Correct 10 ms 29532 KB Output is correct
55 Correct 13 ms 30044 KB Output is correct
56 Correct 31 ms 35280 KB Output is correct
57 Correct 51 ms 37816 KB Output is correct
58 Correct 68 ms 41876 KB Output is correct
59 Correct 112 ms 44660 KB Output is correct
60 Correct 121 ms 47100 KB Output is correct
61 Correct 144 ms 49860 KB Output is correct
62 Correct 186 ms 51508 KB Output is correct
63 Correct 199 ms 54872 KB Output is correct
64 Correct 235 ms 55732 KB Output is correct
65 Correct 95 ms 85088 KB Output is correct
66 Correct 95 ms 85052 KB Output is correct
67 Correct 101 ms 47696 KB Output is correct
68 Correct 147 ms 75056 KB Output is correct
69 Correct 194 ms 72636 KB Output is correct
70 Correct 174 ms 72436 KB Output is correct
71 Correct 214 ms 77188 KB Output is correct
72 Correct 202 ms 77124 KB Output is correct
73 Correct 186 ms 76616 KB Output is correct
74 Correct 187 ms 76876 KB Output is correct
75 Correct 211 ms 74560 KB Output is correct
76 Correct 195 ms 73140 KB Output is correct
77 Correct 188 ms 72572 KB Output is correct
78 Correct 206 ms 72704 KB Output is correct
79 Correct 152 ms 71140 KB Output is correct