Submission #153951

# Submission time Handle Problem Language Result Execution time Memory
153951 2019-09-17T14:50:07 Z songc Fireworks (APIO16_fireworks) C++14
100 / 100
367 ms 75876 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, M;
LL C[303030], S[303030], Y[303030];
vector<int> adj[303030];
priority_queue<LL> PQ[303030];

void dfs(int u){
	if (adj[u].empty()){
		S[u] = -1, Y[u] = C[u];
		PQ[u].push(C[u]);
		PQ[u].push(C[u]);
		return;
	}

	for (int v : adj[u]){
		dfs(v);
		S[u] += S[v], Y[u] += Y[v];
		if (PQ[u].size() < PQ[v].size()) swap(PQ[u], PQ[v]);
		while (!PQ[v].empty()){
			PQ[u].push(PQ[v].top());
			PQ[v].pop();
		}
	}

	LL a=-1, b=-1;
	Y[u] += C[u];
	while (S[u] + PQ[u].size() > 1) PQ[u].pop();
	if (S[u] + PQ[u].size() == 1) a=PQ[u].top(), PQ[u].pop();
	if (S[u] + PQ[u].size() == 0) b=PQ[u].top(), PQ[u].pop();
	if (a != -1) PQ[u].push(a+C[u]);
	if (b != -1) PQ[u].push(b+C[u]);
}

int main(){
	scanf("%d %d", &N, &M);
	for (int i=2; i<=N+M; i++){
		int p;
		scanf("%d %lld", &p, &C[i]);
		adj[p].push_back(i);
	}
	dfs(1);
	LL x = PQ[1].top();
	PQ[1].pop();
	PQ[1].push(0);
	for (int i=0; !PQ[1].empty(); i++){
		Y[1] -= i * (x - PQ[1].top());
		x = PQ[1].top();
		PQ[1].pop();
	}
	printf("%lld\n", Y[1]);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:39: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:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %lld", &p, &C[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 17016 KB Output is correct
2 Correct 17 ms 16888 KB Output is correct
3 Correct 17 ms 16888 KB Output is correct
4 Correct 18 ms 16888 KB Output is correct
5 Correct 18 ms 17016 KB Output is correct
6 Correct 18 ms 17016 KB Output is correct
7 Correct 18 ms 17016 KB Output is correct
8 Correct 18 ms 17016 KB Output is correct
9 Correct 18 ms 16888 KB Output is correct
10 Correct 18 ms 16888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16888 KB Output is correct
2 Correct 18 ms 16888 KB Output is correct
3 Correct 19 ms 17016 KB Output is correct
4 Correct 18 ms 17020 KB Output is correct
5 Correct 19 ms 17016 KB Output is correct
6 Correct 18 ms 17016 KB Output is correct
7 Correct 18 ms 16888 KB Output is correct
8 Correct 18 ms 16888 KB Output is correct
9 Correct 17 ms 17016 KB Output is correct
10 Correct 17 ms 17020 KB Output is correct
11 Correct 18 ms 16888 KB Output is correct
12 Correct 18 ms 16888 KB Output is correct
13 Correct 18 ms 17016 KB Output is correct
14 Correct 25 ms 17016 KB Output is correct
15 Correct 18 ms 17016 KB Output is correct
16 Correct 18 ms 17016 KB Output is correct
17 Correct 18 ms 17016 KB Output is correct
18 Correct 22 ms 16888 KB Output is correct
19 Correct 22 ms 17016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 17016 KB Output is correct
2 Correct 17 ms 16888 KB Output is correct
3 Correct 17 ms 16888 KB Output is correct
4 Correct 18 ms 16888 KB Output is correct
5 Correct 18 ms 17016 KB Output is correct
6 Correct 18 ms 17016 KB Output is correct
7 Correct 18 ms 17016 KB Output is correct
8 Correct 18 ms 17016 KB Output is correct
9 Correct 18 ms 16888 KB Output is correct
10 Correct 18 ms 16888 KB Output is correct
11 Correct 17 ms 16888 KB Output is correct
12 Correct 18 ms 16888 KB Output is correct
13 Correct 19 ms 17016 KB Output is correct
14 Correct 18 ms 17020 KB Output is correct
15 Correct 19 ms 17016 KB Output is correct
16 Correct 18 ms 17016 KB Output is correct
17 Correct 18 ms 16888 KB Output is correct
18 Correct 18 ms 16888 KB Output is correct
19 Correct 17 ms 17016 KB Output is correct
20 Correct 17 ms 17020 KB Output is correct
21 Correct 18 ms 16888 KB Output is correct
22 Correct 18 ms 16888 KB Output is correct
23 Correct 18 ms 17016 KB Output is correct
24 Correct 25 ms 17016 KB Output is correct
25 Correct 18 ms 17016 KB Output is correct
26 Correct 18 ms 17016 KB Output is correct
27 Correct 18 ms 17016 KB Output is correct
28 Correct 22 ms 16888 KB Output is correct
29 Correct 22 ms 17016 KB Output is correct
30 Correct 18 ms 17144 KB Output is correct
31 Correct 18 ms 17016 KB Output is correct
32 Correct 19 ms 17144 KB Output is correct
33 Correct 20 ms 17144 KB Output is correct
34 Correct 20 ms 17144 KB Output is correct
35 Correct 21 ms 17272 KB Output is correct
36 Correct 21 ms 17272 KB Output is correct
37 Correct 21 ms 17400 KB Output is correct
38 Correct 22 ms 17400 KB Output is correct
39 Correct 22 ms 17400 KB Output is correct
40 Correct 24 ms 18040 KB Output is correct
41 Correct 21 ms 17912 KB Output is correct
42 Correct 20 ms 17272 KB Output is correct
43 Correct 22 ms 17656 KB Output is correct
44 Correct 22 ms 17528 KB Output is correct
45 Correct 22 ms 17528 KB Output is correct
46 Correct 22 ms 17400 KB Output is correct
47 Correct 22 ms 17400 KB Output is correct
48 Correct 21 ms 17400 KB Output is correct
49 Correct 22 ms 17456 KB Output is correct
50 Correct 21 ms 17272 KB Output is correct
51 Correct 21 ms 17236 KB Output is correct
52 Correct 21 ms 17272 KB Output is correct
53 Correct 22 ms 17528 KB Output is correct
54 Correct 22 ms 17400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 17016 KB Output is correct
2 Correct 17 ms 16888 KB Output is correct
3 Correct 17 ms 16888 KB Output is correct
4 Correct 18 ms 16888 KB Output is correct
5 Correct 18 ms 17016 KB Output is correct
6 Correct 18 ms 17016 KB Output is correct
7 Correct 18 ms 17016 KB Output is correct
8 Correct 18 ms 17016 KB Output is correct
9 Correct 18 ms 16888 KB Output is correct
10 Correct 18 ms 16888 KB Output is correct
11 Correct 17 ms 16888 KB Output is correct
12 Correct 18 ms 16888 KB Output is correct
13 Correct 19 ms 17016 KB Output is correct
14 Correct 18 ms 17020 KB Output is correct
15 Correct 19 ms 17016 KB Output is correct
16 Correct 18 ms 17016 KB Output is correct
17 Correct 18 ms 16888 KB Output is correct
18 Correct 18 ms 16888 KB Output is correct
19 Correct 17 ms 17016 KB Output is correct
20 Correct 17 ms 17020 KB Output is correct
21 Correct 18 ms 16888 KB Output is correct
22 Correct 18 ms 16888 KB Output is correct
23 Correct 18 ms 17016 KB Output is correct
24 Correct 25 ms 17016 KB Output is correct
25 Correct 18 ms 17016 KB Output is correct
26 Correct 18 ms 17016 KB Output is correct
27 Correct 18 ms 17016 KB Output is correct
28 Correct 22 ms 16888 KB Output is correct
29 Correct 22 ms 17016 KB Output is correct
30 Correct 18 ms 17144 KB Output is correct
31 Correct 18 ms 17016 KB Output is correct
32 Correct 19 ms 17144 KB Output is correct
33 Correct 20 ms 17144 KB Output is correct
34 Correct 20 ms 17144 KB Output is correct
35 Correct 21 ms 17272 KB Output is correct
36 Correct 21 ms 17272 KB Output is correct
37 Correct 21 ms 17400 KB Output is correct
38 Correct 22 ms 17400 KB Output is correct
39 Correct 22 ms 17400 KB Output is correct
40 Correct 24 ms 18040 KB Output is correct
41 Correct 21 ms 17912 KB Output is correct
42 Correct 20 ms 17272 KB Output is correct
43 Correct 22 ms 17656 KB Output is correct
44 Correct 22 ms 17528 KB Output is correct
45 Correct 22 ms 17528 KB Output is correct
46 Correct 22 ms 17400 KB Output is correct
47 Correct 22 ms 17400 KB Output is correct
48 Correct 21 ms 17400 KB Output is correct
49 Correct 22 ms 17456 KB Output is correct
50 Correct 21 ms 17272 KB Output is correct
51 Correct 21 ms 17236 KB Output is correct
52 Correct 21 ms 17272 KB Output is correct
53 Correct 22 ms 17528 KB Output is correct
54 Correct 22 ms 17400 KB Output is correct
55 Correct 28 ms 18040 KB Output is correct
56 Correct 63 ms 21212 KB Output is correct
57 Correct 107 ms 24176 KB Output is correct
58 Correct 167 ms 26224 KB Output is correct
59 Correct 194 ms 29336 KB Output is correct
60 Correct 222 ms 32208 KB Output is correct
61 Correct 251 ms 34804 KB Output is correct
62 Correct 297 ms 36484 KB Output is correct
63 Correct 367 ms 40208 KB Output is correct
64 Correct 354 ms 41380 KB Output is correct
65 Correct 175 ms 75804 KB Output is correct
66 Correct 175 ms 75876 KB Output is correct
67 Correct 201 ms 38520 KB Output is correct
68 Correct 253 ms 58184 KB Output is correct
69 Correct 353 ms 55052 KB Output is correct
70 Correct 333 ms 55028 KB Output is correct
71 Correct 323 ms 50784 KB Output is correct
72 Correct 323 ms 51044 KB Output is correct
73 Correct 269 ms 47324 KB Output is correct
74 Correct 271 ms 47496 KB Output is correct
75 Correct 268 ms 46112 KB Output is correct
76 Correct 268 ms 46308 KB Output is correct
77 Correct 269 ms 45528 KB Output is correct
78 Correct 274 ms 43868 KB Output is correct
79 Correct 270 ms 44544 KB Output is correct