#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxN=3e5;
int n, m, pi, si[mxN];
vector<int> adj[mxN];
ll c[mxN], yi0[mxN], yi1[mxN];
priority_queue<ll> pc[mxN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=1; i<n+m; ++i) {
cin >> pi >> c[i], --pi;
adj[pi].push_back(i);
}
for(int i=n+m-1; i>=0; --i) {
if(i>=n) {
yi1[i]=-1;
pc[i].push(0);
pc[i].push(0);
si[i]=i;
} else {
int mj=adj[i][0];
for(int j : adj[i])
if(pc[si[j]].size()<pc[si[j]].size())
mj=j;
si[i]=si[mj];
for(int j : adj[i]) {
yi0[i]+=yi0[j];
yi1[i]+=yi1[j];
if(j==mj)
continue;
while(!pc[si[j]].empty()) {
pc[si[i]].push(pc[si[j]].top());
pc[si[j]].pop();
}
}
}
while(pc[si[i]].size()>-yi1[i]+1)
pc[si[i]].pop();
ll a1=pc[si[i]].top();
pc[si[i]].pop();
ll a2=pc[si[i]].top();
pc[si[i]].pop();
pc[si[i]].push(a1+c[i]);
pc[si[i]].push(a2+c[i]);
yi0[i]+=c[i];
// cout << yi0[i] << " " << yi1[i] << endl;
}
ll ans=yi0[0];
pc[si[0]].pop();
pc[si[0]].push(0);
while(pc[si[0]].size()>1) {
ans+=(yi1[0]+pc[si[0]].size()-2)*pc[si[0]].top();
pc[si[0]].pop();
ans-=(yi1[0]+pc[si[0]].size()-1)*pc[si[0]].top();
}
cout << ans;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:44:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pc[si[i]].size()>-yi1[i]+1)
~~~~~~~~~~~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16992 KB |
Output is correct |
3 |
Correct |
15 ms |
16992 KB |
Output is correct |
4 |
Correct |
15 ms |
16992 KB |
Output is correct |
5 |
Correct |
18 ms |
16992 KB |
Output is correct |
6 |
Correct |
19 ms |
17084 KB |
Output is correct |
7 |
Correct |
18 ms |
17084 KB |
Output is correct |
8 |
Correct |
16 ms |
17084 KB |
Output is correct |
9 |
Correct |
16 ms |
17160 KB |
Output is correct |
10 |
Correct |
15 ms |
17160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
17160 KB |
Output is correct |
2 |
Correct |
15 ms |
17160 KB |
Output is correct |
3 |
Correct |
15 ms |
17160 KB |
Output is correct |
4 |
Correct |
14 ms |
17160 KB |
Output is correct |
5 |
Correct |
16 ms |
17160 KB |
Output is correct |
6 |
Correct |
16 ms |
17160 KB |
Output is correct |
7 |
Correct |
15 ms |
17160 KB |
Output is correct |
8 |
Correct |
16 ms |
17160 KB |
Output is correct |
9 |
Correct |
15 ms |
17160 KB |
Output is correct |
10 |
Correct |
15 ms |
17160 KB |
Output is correct |
11 |
Correct |
15 ms |
17160 KB |
Output is correct |
12 |
Correct |
15 ms |
17160 KB |
Output is correct |
13 |
Correct |
17 ms |
17160 KB |
Output is correct |
14 |
Correct |
17 ms |
17160 KB |
Output is correct |
15 |
Correct |
17 ms |
17160 KB |
Output is correct |
16 |
Correct |
15 ms |
17160 KB |
Output is correct |
17 |
Correct |
17 ms |
17160 KB |
Output is correct |
18 |
Correct |
15 ms |
17160 KB |
Output is correct |
19 |
Correct |
15 ms |
17160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16992 KB |
Output is correct |
3 |
Correct |
15 ms |
16992 KB |
Output is correct |
4 |
Correct |
15 ms |
16992 KB |
Output is correct |
5 |
Correct |
18 ms |
16992 KB |
Output is correct |
6 |
Correct |
19 ms |
17084 KB |
Output is correct |
7 |
Correct |
18 ms |
17084 KB |
Output is correct |
8 |
Correct |
16 ms |
17084 KB |
Output is correct |
9 |
Correct |
16 ms |
17160 KB |
Output is correct |
10 |
Correct |
15 ms |
17160 KB |
Output is correct |
11 |
Correct |
15 ms |
17160 KB |
Output is correct |
12 |
Correct |
15 ms |
17160 KB |
Output is correct |
13 |
Correct |
15 ms |
17160 KB |
Output is correct |
14 |
Correct |
14 ms |
17160 KB |
Output is correct |
15 |
Correct |
16 ms |
17160 KB |
Output is correct |
16 |
Correct |
16 ms |
17160 KB |
Output is correct |
17 |
Correct |
15 ms |
17160 KB |
Output is correct |
18 |
Correct |
16 ms |
17160 KB |
Output is correct |
19 |
Correct |
15 ms |
17160 KB |
Output is correct |
20 |
Correct |
15 ms |
17160 KB |
Output is correct |
21 |
Correct |
15 ms |
17160 KB |
Output is correct |
22 |
Correct |
15 ms |
17160 KB |
Output is correct |
23 |
Correct |
17 ms |
17160 KB |
Output is correct |
24 |
Correct |
17 ms |
17160 KB |
Output is correct |
25 |
Correct |
17 ms |
17160 KB |
Output is correct |
26 |
Correct |
15 ms |
17160 KB |
Output is correct |
27 |
Correct |
17 ms |
17160 KB |
Output is correct |
28 |
Correct |
15 ms |
17160 KB |
Output is correct |
29 |
Correct |
15 ms |
17160 KB |
Output is correct |
30 |
Correct |
16 ms |
17160 KB |
Output is correct |
31 |
Correct |
15 ms |
17160 KB |
Output is correct |
32 |
Correct |
16 ms |
17160 KB |
Output is correct |
33 |
Correct |
18 ms |
17276 KB |
Output is correct |
34 |
Correct |
17 ms |
17328 KB |
Output is correct |
35 |
Correct |
17 ms |
17328 KB |
Output is correct |
36 |
Correct |
19 ms |
17404 KB |
Output is correct |
37 |
Correct |
19 ms |
17404 KB |
Output is correct |
38 |
Correct |
18 ms |
17408 KB |
Output is correct |
39 |
Correct |
18 ms |
17408 KB |
Output is correct |
40 |
Correct |
17 ms |
17408 KB |
Output is correct |
41 |
Correct |
16 ms |
17408 KB |
Output is correct |
42 |
Correct |
19 ms |
17408 KB |
Output is correct |
43 |
Correct |
21 ms |
17520 KB |
Output is correct |
44 |
Correct |
18 ms |
17520 KB |
Output is correct |
45 |
Correct |
21 ms |
17520 KB |
Output is correct |
46 |
Correct |
23 ms |
17648 KB |
Output is correct |
47 |
Correct |
22 ms |
17648 KB |
Output is correct |
48 |
Correct |
19 ms |
17648 KB |
Output is correct |
49 |
Correct |
19 ms |
17648 KB |
Output is correct |
50 |
Correct |
23 ms |
17648 KB |
Output is correct |
51 |
Correct |
18 ms |
17648 KB |
Output is correct |
52 |
Correct |
20 ms |
17648 KB |
Output is correct |
53 |
Correct |
22 ms |
17648 KB |
Output is correct |
54 |
Correct |
22 ms |
17648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16992 KB |
Output is correct |
3 |
Correct |
15 ms |
16992 KB |
Output is correct |
4 |
Correct |
15 ms |
16992 KB |
Output is correct |
5 |
Correct |
18 ms |
16992 KB |
Output is correct |
6 |
Correct |
19 ms |
17084 KB |
Output is correct |
7 |
Correct |
18 ms |
17084 KB |
Output is correct |
8 |
Correct |
16 ms |
17084 KB |
Output is correct |
9 |
Correct |
16 ms |
17160 KB |
Output is correct |
10 |
Correct |
15 ms |
17160 KB |
Output is correct |
11 |
Correct |
15 ms |
17160 KB |
Output is correct |
12 |
Correct |
15 ms |
17160 KB |
Output is correct |
13 |
Correct |
15 ms |
17160 KB |
Output is correct |
14 |
Correct |
14 ms |
17160 KB |
Output is correct |
15 |
Correct |
16 ms |
17160 KB |
Output is correct |
16 |
Correct |
16 ms |
17160 KB |
Output is correct |
17 |
Correct |
15 ms |
17160 KB |
Output is correct |
18 |
Correct |
16 ms |
17160 KB |
Output is correct |
19 |
Correct |
15 ms |
17160 KB |
Output is correct |
20 |
Correct |
15 ms |
17160 KB |
Output is correct |
21 |
Correct |
15 ms |
17160 KB |
Output is correct |
22 |
Correct |
15 ms |
17160 KB |
Output is correct |
23 |
Correct |
17 ms |
17160 KB |
Output is correct |
24 |
Correct |
17 ms |
17160 KB |
Output is correct |
25 |
Correct |
17 ms |
17160 KB |
Output is correct |
26 |
Correct |
15 ms |
17160 KB |
Output is correct |
27 |
Correct |
17 ms |
17160 KB |
Output is correct |
28 |
Correct |
15 ms |
17160 KB |
Output is correct |
29 |
Correct |
15 ms |
17160 KB |
Output is correct |
30 |
Correct |
16 ms |
17160 KB |
Output is correct |
31 |
Correct |
15 ms |
17160 KB |
Output is correct |
32 |
Correct |
16 ms |
17160 KB |
Output is correct |
33 |
Correct |
18 ms |
17276 KB |
Output is correct |
34 |
Correct |
17 ms |
17328 KB |
Output is correct |
35 |
Correct |
17 ms |
17328 KB |
Output is correct |
36 |
Correct |
19 ms |
17404 KB |
Output is correct |
37 |
Correct |
19 ms |
17404 KB |
Output is correct |
38 |
Correct |
18 ms |
17408 KB |
Output is correct |
39 |
Correct |
18 ms |
17408 KB |
Output is correct |
40 |
Correct |
17 ms |
17408 KB |
Output is correct |
41 |
Correct |
16 ms |
17408 KB |
Output is correct |
42 |
Correct |
19 ms |
17408 KB |
Output is correct |
43 |
Correct |
21 ms |
17520 KB |
Output is correct |
44 |
Correct |
18 ms |
17520 KB |
Output is correct |
45 |
Correct |
21 ms |
17520 KB |
Output is correct |
46 |
Correct |
23 ms |
17648 KB |
Output is correct |
47 |
Correct |
22 ms |
17648 KB |
Output is correct |
48 |
Correct |
19 ms |
17648 KB |
Output is correct |
49 |
Correct |
19 ms |
17648 KB |
Output is correct |
50 |
Correct |
23 ms |
17648 KB |
Output is correct |
51 |
Correct |
18 ms |
17648 KB |
Output is correct |
52 |
Correct |
20 ms |
17648 KB |
Output is correct |
53 |
Correct |
22 ms |
17648 KB |
Output is correct |
54 |
Correct |
22 ms |
17648 KB |
Output is correct |
55 |
Correct |
23 ms |
18172 KB |
Output is correct |
56 |
Correct |
63 ms |
21244 KB |
Output is correct |
57 |
Correct |
120 ms |
24064 KB |
Output is correct |
58 |
Correct |
127 ms |
26128 KB |
Output is correct |
59 |
Correct |
199 ms |
28896 KB |
Output is correct |
60 |
Correct |
295 ms |
31720 KB |
Output is correct |
61 |
Correct |
263 ms |
34356 KB |
Output is correct |
62 |
Correct |
343 ms |
35964 KB |
Output is correct |
63 |
Correct |
324 ms |
39484 KB |
Output is correct |
64 |
Correct |
327 ms |
40408 KB |
Output is correct |
65 |
Correct |
126 ms |
40408 KB |
Output is correct |
66 |
Correct |
141 ms |
40408 KB |
Output is correct |
67 |
Correct |
171 ms |
40408 KB |
Output is correct |
68 |
Correct |
244 ms |
40408 KB |
Output is correct |
69 |
Correct |
265 ms |
40408 KB |
Output is correct |
70 |
Correct |
263 ms |
40408 KB |
Output is correct |
71 |
Correct |
381 ms |
50092 KB |
Output is correct |
72 |
Correct |
321 ms |
50168 KB |
Output is correct |
73 |
Correct |
263 ms |
50168 KB |
Output is correct |
74 |
Correct |
252 ms |
50168 KB |
Output is correct |
75 |
Correct |
266 ms |
50168 KB |
Output is correct |
76 |
Correct |
282 ms |
50168 KB |
Output is correct |
77 |
Correct |
293 ms |
50168 KB |
Output is correct |
78 |
Correct |
279 ms |
50168 KB |
Output is correct |
79 |
Correct |
273 ms |
50168 KB |
Output is correct |