Submission #113718

# Submission time Handle Problem Language Result Execution time Memory
113718 2019-05-28T03:38:25 Z imyujin Fireworks (APIO16_fireworks) C++14
100 / 100
364 ms 82808 KB
#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>

using namespace std;

#define MAXN 300005

int n, m;
int p[MAXN];
long long c[MAXN];
int sz[MAXN];
vector<int> gph[MAXN];

struct func{
	priority_queue<long long> pq;
	long long cost;
	int slope;

	void init(){
		cost=0;
		slope=-1;
		pq.push(0);
		pq.push(0);
	}

	void upperize(int x){
		vector<long long> v;
		cost+=c[x];
		while(!pq.empty()&&slope+(int)pq.size()>1) pq.pop();
		while(!pq.empty()&&slope+(int)pq.size()>=0){
			v.push_back(pq.top());
			pq.pop();
		}
		while(!v.empty()){
			pq.push(v.back()+c[x]);
			v.pop_back();
		}
	}
}dp[MAXN];

bool cmp(int a, int b){
	return sz[a]>sz[b];
}

void dfs(int x){
	if(x>n){
		sz[x]=1;
		return;
	}
	for(int i=0; i<gph[x].size(); i++){
		dfs(gph[x][i]);
		sz[x]+=sz[gph[x][i]];
	}
	sort(gph[x].begin(), gph[x].end(), cmp);
}

int solve(int x){
	if(x>n){
		dp[x].init();
		return x;
	}

	int ret=solve(gph[x][0]);
	dp[ret].upperize(gph[x][0]);
	for(int i=1; i<gph[x].size(); i++){
		int t=solve(gph[x][i]);
		dp[t].upperize(gph[x][i]);
		dp[ret].cost+=dp[t].cost;
		dp[ret].slope+=dp[t].slope;
		while(!dp[t].pq.empty()){
			dp[ret].pq.push(dp[t].pq.top());
			dp[t].pq.pop();
		}
	}
	return ret;
}

int main(){
	scanf("%d%d", &n, &m);
	for(int i=2; i<=n+m; i++) scanf("%d%lld", p+i, c+i);
	
	for(int i=2; i<=n+m; i++) gph[p[i]].push_back(i);
	dfs(1);
	func ret=dp[solve(1)];
	ret.upperize(0);
	long long ansp=ret.pq.top();
	long long ans=ret.cost+ansp*ret.slope;
	while(!ret.pq.empty()){
		ans+=ansp-ret.pq.top();
		ret.pq.pop();
	}

	printf("%lld", ans);
	return 0;
}

Compilation message

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:52:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gph[x].size(); i++){
               ~^~~~~~~~~~~~~~
fireworks.cpp: In function 'int solve(int)':
fireworks.cpp:67:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1; i<gph[x].size(); i++){
               ~^~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:82:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=2; i<=n+m; i++) scanf("%d%lld", p+i, c+i);
                            ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 21504 KB Output is correct
2 Correct 19 ms 21504 KB Output is correct
3 Correct 19 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 18 ms 21504 KB Output is correct
6 Correct 19 ms 21504 KB Output is correct
7 Correct 19 ms 21504 KB Output is correct
8 Correct 19 ms 21504 KB Output is correct
9 Correct 19 ms 21504 KB Output is correct
10 Correct 18 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 21504 KB Output is correct
2 Correct 18 ms 21504 KB Output is correct
3 Correct 19 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 18 ms 21504 KB Output is correct
6 Correct 21 ms 21504 KB Output is correct
7 Correct 19 ms 21504 KB Output is correct
8 Correct 18 ms 21504 KB Output is correct
9 Correct 20 ms 21504 KB Output is correct
10 Correct 18 ms 21504 KB Output is correct
11 Correct 18 ms 21504 KB Output is correct
12 Correct 19 ms 21504 KB Output is correct
13 Correct 19 ms 21504 KB Output is correct
14 Correct 18 ms 21504 KB Output is correct
15 Correct 18 ms 21504 KB Output is correct
16 Correct 18 ms 21504 KB Output is correct
17 Correct 18 ms 21504 KB Output is correct
18 Correct 18 ms 21504 KB Output is correct
19 Correct 18 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 21504 KB Output is correct
2 Correct 19 ms 21504 KB Output is correct
3 Correct 19 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 18 ms 21504 KB Output is correct
6 Correct 19 ms 21504 KB Output is correct
7 Correct 19 ms 21504 KB Output is correct
8 Correct 19 ms 21504 KB Output is correct
9 Correct 19 ms 21504 KB Output is correct
10 Correct 18 ms 21504 KB Output is correct
11 Correct 18 ms 21504 KB Output is correct
12 Correct 18 ms 21504 KB Output is correct
13 Correct 19 ms 21504 KB Output is correct
14 Correct 18 ms 21504 KB Output is correct
15 Correct 18 ms 21504 KB Output is correct
16 Correct 21 ms 21504 KB Output is correct
17 Correct 19 ms 21504 KB Output is correct
18 Correct 18 ms 21504 KB Output is correct
19 Correct 20 ms 21504 KB Output is correct
20 Correct 18 ms 21504 KB Output is correct
21 Correct 18 ms 21504 KB Output is correct
22 Correct 19 ms 21504 KB Output is correct
23 Correct 19 ms 21504 KB Output is correct
24 Correct 18 ms 21504 KB Output is correct
25 Correct 18 ms 21504 KB Output is correct
26 Correct 18 ms 21504 KB Output is correct
27 Correct 18 ms 21504 KB Output is correct
28 Correct 18 ms 21504 KB Output is correct
29 Correct 18 ms 21504 KB Output is correct
30 Correct 19 ms 21504 KB Output is correct
31 Correct 19 ms 21596 KB Output is correct
32 Correct 19 ms 21504 KB Output is correct
33 Correct 20 ms 21624 KB Output is correct
34 Correct 22 ms 21760 KB Output is correct
35 Correct 21 ms 21880 KB Output is correct
36 Correct 23 ms 21760 KB Output is correct
37 Correct 23 ms 21888 KB Output is correct
38 Correct 22 ms 21888 KB Output is correct
39 Correct 23 ms 21888 KB Output is correct
40 Correct 23 ms 22400 KB Output is correct
41 Correct 23 ms 22400 KB Output is correct
42 Correct 22 ms 21888 KB Output is correct
43 Correct 23 ms 22144 KB Output is correct
44 Correct 23 ms 22016 KB Output is correct
45 Correct 23 ms 22100 KB Output is correct
46 Correct 23 ms 22016 KB Output is correct
47 Correct 24 ms 22016 KB Output is correct
48 Correct 23 ms 21888 KB Output is correct
49 Correct 24 ms 21880 KB Output is correct
50 Correct 23 ms 21760 KB Output is correct
51 Correct 23 ms 21760 KB Output is correct
52 Correct 23 ms 21888 KB Output is correct
53 Correct 23 ms 21888 KB Output is correct
54 Correct 23 ms 22016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 21504 KB Output is correct
2 Correct 19 ms 21504 KB Output is correct
3 Correct 19 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 18 ms 21504 KB Output is correct
6 Correct 19 ms 21504 KB Output is correct
7 Correct 19 ms 21504 KB Output is correct
8 Correct 19 ms 21504 KB Output is correct
9 Correct 19 ms 21504 KB Output is correct
10 Correct 18 ms 21504 KB Output is correct
11 Correct 18 ms 21504 KB Output is correct
12 Correct 18 ms 21504 KB Output is correct
13 Correct 19 ms 21504 KB Output is correct
14 Correct 18 ms 21504 KB Output is correct
15 Correct 18 ms 21504 KB Output is correct
16 Correct 21 ms 21504 KB Output is correct
17 Correct 19 ms 21504 KB Output is correct
18 Correct 18 ms 21504 KB Output is correct
19 Correct 20 ms 21504 KB Output is correct
20 Correct 18 ms 21504 KB Output is correct
21 Correct 18 ms 21504 KB Output is correct
22 Correct 19 ms 21504 KB Output is correct
23 Correct 19 ms 21504 KB Output is correct
24 Correct 18 ms 21504 KB Output is correct
25 Correct 18 ms 21504 KB Output is correct
26 Correct 18 ms 21504 KB Output is correct
27 Correct 18 ms 21504 KB Output is correct
28 Correct 18 ms 21504 KB Output is correct
29 Correct 18 ms 21504 KB Output is correct
30 Correct 19 ms 21504 KB Output is correct
31 Correct 19 ms 21596 KB Output is correct
32 Correct 19 ms 21504 KB Output is correct
33 Correct 20 ms 21624 KB Output is correct
34 Correct 22 ms 21760 KB Output is correct
35 Correct 21 ms 21880 KB Output is correct
36 Correct 23 ms 21760 KB Output is correct
37 Correct 23 ms 21888 KB Output is correct
38 Correct 22 ms 21888 KB Output is correct
39 Correct 23 ms 21888 KB Output is correct
40 Correct 23 ms 22400 KB Output is correct
41 Correct 23 ms 22400 KB Output is correct
42 Correct 22 ms 21888 KB Output is correct
43 Correct 23 ms 22144 KB Output is correct
44 Correct 23 ms 22016 KB Output is correct
45 Correct 23 ms 22100 KB Output is correct
46 Correct 23 ms 22016 KB Output is correct
47 Correct 24 ms 22016 KB Output is correct
48 Correct 23 ms 21888 KB Output is correct
49 Correct 24 ms 21880 KB Output is correct
50 Correct 23 ms 21760 KB Output is correct
51 Correct 23 ms 21760 KB Output is correct
52 Correct 23 ms 21888 KB Output is correct
53 Correct 23 ms 21888 KB Output is correct
54 Correct 23 ms 22016 KB Output is correct
55 Correct 29 ms 22528 KB Output is correct
56 Correct 65 ms 25460 KB Output is correct
57 Correct 124 ms 28468 KB Output is correct
58 Correct 128 ms 30164 KB Output is correct
59 Correct 175 ms 33092 KB Output is correct
60 Correct 233 ms 36028 KB Output is correct
61 Correct 250 ms 37992 KB Output is correct
62 Correct 306 ms 39836 KB Output is correct
63 Correct 354 ms 43528 KB Output is correct
64 Correct 364 ms 44460 KB Output is correct
65 Correct 168 ms 82800 KB Output is correct
66 Correct 169 ms 82808 KB Output is correct
67 Correct 236 ms 40696 KB Output is correct
68 Correct 255 ms 63852 KB Output is correct
69 Correct 297 ms 60260 KB Output is correct
70 Correct 295 ms 60380 KB Output is correct
71 Correct 301 ms 55268 KB Output is correct
72 Correct 300 ms 55388 KB Output is correct
73 Correct 308 ms 51944 KB Output is correct
74 Correct 277 ms 51936 KB Output is correct
75 Correct 272 ms 50756 KB Output is correct
76 Correct 284 ms 50784 KB Output is correct
77 Correct 275 ms 49524 KB Output is correct
78 Correct 287 ms 48356 KB Output is correct
79 Correct 289 ms 49748 KB Output is correct