#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pli pair<ll, int>
#define fi first
#define se second
const int mxN=3e5;
int n, m, pi, ui, si[mxN];
vector<int> adj[mxN];
ll c[mxN], yi0[mxN], yi1[mxN];
set<pli> 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].insert({0, ui++});
pc[i].insert({0, ui++});
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)
pc[si[i]].insert(pc[si[j]].begin(), pc[si[j]].end());
}
}
auto it=pc[si[i]].end();
while(pc[si[i]].size()>-yi1[i]+1)
it=pc[si[i]].erase(--it);
pli p1=*--it;
it=pc[si[i]].erase(it);
pli p2=*--it;
it=pc[si[i]].erase(it);
it=pc[si[i]].insert(it, {p2.fi+c[i], p2.se});
it=pc[si[i]].insert(it, {p1.fi+c[i], p1.se});
yi0[i]+=c[i];
// cout << yi0[i] << " " << yi1[i] << endl;
}
ll ans=yi0[0];
auto it=pc[si[0]].insert({0, ui++}).fi;
for(int i=0; i<-yi1[0]; ++i) {
ans-=(yi1[0]+i)*it->fi;
ans+=(yi1[0]+i)*(++it)->fi;
}
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 |
19 ms |
21496 KB |
Output is correct |
2 |
Correct |
21 ms |
21496 KB |
Output is correct |
3 |
Correct |
20 ms |
21676 KB |
Output is correct |
4 |
Correct |
19 ms |
21676 KB |
Output is correct |
5 |
Correct |
21 ms |
21676 KB |
Output is correct |
6 |
Correct |
19 ms |
21676 KB |
Output is correct |
7 |
Correct |
18 ms |
21676 KB |
Output is correct |
8 |
Correct |
26 ms |
21792 KB |
Output is correct |
9 |
Correct |
22 ms |
21792 KB |
Output is correct |
10 |
Correct |
26 ms |
21792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21792 KB |
Output is correct |
2 |
Correct |
19 ms |
21792 KB |
Output is correct |
3 |
Correct |
22 ms |
21792 KB |
Output is correct |
4 |
Correct |
19 ms |
21792 KB |
Output is correct |
5 |
Correct |
22 ms |
21792 KB |
Output is correct |
6 |
Correct |
19 ms |
21820 KB |
Output is correct |
7 |
Correct |
22 ms |
21820 KB |
Output is correct |
8 |
Correct |
20 ms |
21820 KB |
Output is correct |
9 |
Correct |
19 ms |
21820 KB |
Output is correct |
10 |
Correct |
19 ms |
21820 KB |
Output is correct |
11 |
Correct |
19 ms |
21820 KB |
Output is correct |
12 |
Correct |
22 ms |
21820 KB |
Output is correct |
13 |
Correct |
20 ms |
21820 KB |
Output is correct |
14 |
Correct |
20 ms |
21820 KB |
Output is correct |
15 |
Correct |
19 ms |
21820 KB |
Output is correct |
16 |
Correct |
19 ms |
21820 KB |
Output is correct |
17 |
Correct |
20 ms |
21820 KB |
Output is correct |
18 |
Correct |
19 ms |
21820 KB |
Output is correct |
19 |
Correct |
19 ms |
21820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
21496 KB |
Output is correct |
2 |
Correct |
21 ms |
21496 KB |
Output is correct |
3 |
Correct |
20 ms |
21676 KB |
Output is correct |
4 |
Correct |
19 ms |
21676 KB |
Output is correct |
5 |
Correct |
21 ms |
21676 KB |
Output is correct |
6 |
Correct |
19 ms |
21676 KB |
Output is correct |
7 |
Correct |
18 ms |
21676 KB |
Output is correct |
8 |
Correct |
26 ms |
21792 KB |
Output is correct |
9 |
Correct |
22 ms |
21792 KB |
Output is correct |
10 |
Correct |
26 ms |
21792 KB |
Output is correct |
11 |
Correct |
21 ms |
21792 KB |
Output is correct |
12 |
Correct |
19 ms |
21792 KB |
Output is correct |
13 |
Correct |
22 ms |
21792 KB |
Output is correct |
14 |
Correct |
19 ms |
21792 KB |
Output is correct |
15 |
Correct |
22 ms |
21792 KB |
Output is correct |
16 |
Correct |
19 ms |
21820 KB |
Output is correct |
17 |
Correct |
22 ms |
21820 KB |
Output is correct |
18 |
Correct |
20 ms |
21820 KB |
Output is correct |
19 |
Correct |
19 ms |
21820 KB |
Output is correct |
20 |
Correct |
19 ms |
21820 KB |
Output is correct |
21 |
Correct |
19 ms |
21820 KB |
Output is correct |
22 |
Correct |
22 ms |
21820 KB |
Output is correct |
23 |
Correct |
20 ms |
21820 KB |
Output is correct |
24 |
Correct |
20 ms |
21820 KB |
Output is correct |
25 |
Correct |
19 ms |
21820 KB |
Output is correct |
26 |
Correct |
19 ms |
21820 KB |
Output is correct |
27 |
Correct |
20 ms |
21820 KB |
Output is correct |
28 |
Correct |
19 ms |
21820 KB |
Output is correct |
29 |
Correct |
19 ms |
21820 KB |
Output is correct |
30 |
Correct |
20 ms |
21820 KB |
Output is correct |
31 |
Correct |
19 ms |
21884 KB |
Output is correct |
32 |
Correct |
20 ms |
22012 KB |
Output is correct |
33 |
Correct |
22 ms |
22140 KB |
Output is correct |
34 |
Correct |
22 ms |
22144 KB |
Output is correct |
35 |
Correct |
28 ms |
22252 KB |
Output is correct |
36 |
Correct |
27 ms |
22396 KB |
Output is correct |
37 |
Correct |
31 ms |
22472 KB |
Output is correct |
38 |
Correct |
24 ms |
22524 KB |
Output is correct |
39 |
Correct |
24 ms |
22524 KB |
Output is correct |
40 |
Correct |
23 ms |
22524 KB |
Output is correct |
41 |
Correct |
23 ms |
22524 KB |
Output is correct |
42 |
Correct |
22 ms |
22524 KB |
Output is correct |
43 |
Correct |
28 ms |
22524 KB |
Output is correct |
44 |
Correct |
28 ms |
22524 KB |
Output is correct |
45 |
Correct |
26 ms |
22524 KB |
Output is correct |
46 |
Correct |
28 ms |
23292 KB |
Output is correct |
47 |
Correct |
26 ms |
23292 KB |
Output is correct |
48 |
Correct |
27 ms |
23292 KB |
Output is correct |
49 |
Correct |
25 ms |
23292 KB |
Output is correct |
50 |
Correct |
24 ms |
23292 KB |
Output is correct |
51 |
Correct |
24 ms |
23292 KB |
Output is correct |
52 |
Correct |
24 ms |
23292 KB |
Output is correct |
53 |
Correct |
26 ms |
23292 KB |
Output is correct |
54 |
Correct |
26 ms |
23292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
21496 KB |
Output is correct |
2 |
Correct |
21 ms |
21496 KB |
Output is correct |
3 |
Correct |
20 ms |
21676 KB |
Output is correct |
4 |
Correct |
19 ms |
21676 KB |
Output is correct |
5 |
Correct |
21 ms |
21676 KB |
Output is correct |
6 |
Correct |
19 ms |
21676 KB |
Output is correct |
7 |
Correct |
18 ms |
21676 KB |
Output is correct |
8 |
Correct |
26 ms |
21792 KB |
Output is correct |
9 |
Correct |
22 ms |
21792 KB |
Output is correct |
10 |
Correct |
26 ms |
21792 KB |
Output is correct |
11 |
Correct |
21 ms |
21792 KB |
Output is correct |
12 |
Correct |
19 ms |
21792 KB |
Output is correct |
13 |
Correct |
22 ms |
21792 KB |
Output is correct |
14 |
Correct |
19 ms |
21792 KB |
Output is correct |
15 |
Correct |
22 ms |
21792 KB |
Output is correct |
16 |
Correct |
19 ms |
21820 KB |
Output is correct |
17 |
Correct |
22 ms |
21820 KB |
Output is correct |
18 |
Correct |
20 ms |
21820 KB |
Output is correct |
19 |
Correct |
19 ms |
21820 KB |
Output is correct |
20 |
Correct |
19 ms |
21820 KB |
Output is correct |
21 |
Correct |
19 ms |
21820 KB |
Output is correct |
22 |
Correct |
22 ms |
21820 KB |
Output is correct |
23 |
Correct |
20 ms |
21820 KB |
Output is correct |
24 |
Correct |
20 ms |
21820 KB |
Output is correct |
25 |
Correct |
19 ms |
21820 KB |
Output is correct |
26 |
Correct |
19 ms |
21820 KB |
Output is correct |
27 |
Correct |
20 ms |
21820 KB |
Output is correct |
28 |
Correct |
19 ms |
21820 KB |
Output is correct |
29 |
Correct |
19 ms |
21820 KB |
Output is correct |
30 |
Correct |
20 ms |
21820 KB |
Output is correct |
31 |
Correct |
19 ms |
21884 KB |
Output is correct |
32 |
Correct |
20 ms |
22012 KB |
Output is correct |
33 |
Correct |
22 ms |
22140 KB |
Output is correct |
34 |
Correct |
22 ms |
22144 KB |
Output is correct |
35 |
Correct |
28 ms |
22252 KB |
Output is correct |
36 |
Correct |
27 ms |
22396 KB |
Output is correct |
37 |
Correct |
31 ms |
22472 KB |
Output is correct |
38 |
Correct |
24 ms |
22524 KB |
Output is correct |
39 |
Correct |
24 ms |
22524 KB |
Output is correct |
40 |
Correct |
23 ms |
22524 KB |
Output is correct |
41 |
Correct |
23 ms |
22524 KB |
Output is correct |
42 |
Correct |
22 ms |
22524 KB |
Output is correct |
43 |
Correct |
28 ms |
22524 KB |
Output is correct |
44 |
Correct |
28 ms |
22524 KB |
Output is correct |
45 |
Correct |
26 ms |
22524 KB |
Output is correct |
46 |
Correct |
28 ms |
23292 KB |
Output is correct |
47 |
Correct |
26 ms |
23292 KB |
Output is correct |
48 |
Correct |
27 ms |
23292 KB |
Output is correct |
49 |
Correct |
25 ms |
23292 KB |
Output is correct |
50 |
Correct |
24 ms |
23292 KB |
Output is correct |
51 |
Correct |
24 ms |
23292 KB |
Output is correct |
52 |
Correct |
24 ms |
23292 KB |
Output is correct |
53 |
Correct |
26 ms |
23292 KB |
Output is correct |
54 |
Correct |
26 ms |
23292 KB |
Output is correct |
55 |
Correct |
31 ms |
23932 KB |
Output is correct |
56 |
Correct |
103 ms |
30332 KB |
Output is correct |
57 |
Correct |
137 ms |
37116 KB |
Output is correct |
58 |
Correct |
214 ms |
41752 KB |
Output is correct |
59 |
Correct |
257 ms |
48380 KB |
Output is correct |
60 |
Correct |
376 ms |
55548 KB |
Output is correct |
61 |
Correct |
399 ms |
60348 KB |
Output is correct |
62 |
Correct |
481 ms |
67740 KB |
Output is correct |
63 |
Correct |
551 ms |
82516 KB |
Output is correct |
64 |
Correct |
522 ms |
86856 KB |
Output is correct |
65 |
Correct |
167 ms |
86856 KB |
Output is correct |
66 |
Correct |
163 ms |
86856 KB |
Output is correct |
67 |
Correct |
168 ms |
86856 KB |
Output is correct |
68 |
Correct |
385 ms |
94484 KB |
Output is correct |
69 |
Correct |
493 ms |
99700 KB |
Output is correct |
70 |
Correct |
531 ms |
104508 KB |
Output is correct |
71 |
Correct |
693 ms |
169524 KB |
Output is correct |
72 |
Correct |
719 ms |
173352 KB |
Output is correct |
73 |
Correct |
732 ms |
173352 KB |
Output is correct |
74 |
Correct |
642 ms |
173352 KB |
Output is correct |
75 |
Correct |
680 ms |
173352 KB |
Output is correct |
76 |
Correct |
628 ms |
173352 KB |
Output is correct |
77 |
Correct |
722 ms |
173580 KB |
Output is correct |
78 |
Correct |
761 ms |
184876 KB |
Output is correct |
79 |
Correct |
752 ms |
184876 KB |
Output is correct |