#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline int in() {
int result = 0;
char ch = getchar_unlocked();
while (true) {
if(ch >= '0' && ch <= '9')
break;
ch = getchar_unlocked();
}
result = ch-'0';
while(true) {
ch = getchar_unlocked();
if (ch < '0' || ch > '9')
break;
result = result*10 + (ch - '0');
}
return result;
}
inline void out(ll x) {
ll rev=x;
int c=0;
if(!x) {
putchar_unlocked('0');
return;
}
while(!(rev%10)) {
++c;
rev/=10;
}
rev=0;
while(x) {
rev=rev*10+x%10;
x/=10;
}
while(rev) {
putchar_unlocked(rev%10+'0');
rev/=10;
}
while(c--)
putchar_unlocked('0');
}
const int mxN=3e5;
int n, m, c[mxN], si[mxN];
vector<int> adj[mxN];
ll yi0[mxN], yi1[mxN];
priority_queue<ll> pc[mxN];
int main() {
n=in(), m=in();
for(int i=1; i<n+m; ++i) {
adj[in()-1].push_back(i);
c[i]=in();
}
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];
}
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();
}
out(ans);
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:82: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 |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16872 KB |
Output is correct |
3 |
Correct |
16 ms |
16948 KB |
Output is correct |
4 |
Correct |
17 ms |
16948 KB |
Output is correct |
5 |
Correct |
18 ms |
17048 KB |
Output is correct |
6 |
Correct |
18 ms |
17048 KB |
Output is correct |
7 |
Correct |
16 ms |
17048 KB |
Output is correct |
8 |
Correct |
21 ms |
17048 KB |
Output is correct |
9 |
Correct |
16 ms |
17048 KB |
Output is correct |
10 |
Correct |
16 ms |
17048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
17048 KB |
Output is correct |
2 |
Correct |
17 ms |
17048 KB |
Output is correct |
3 |
Correct |
15 ms |
17048 KB |
Output is correct |
4 |
Correct |
18 ms |
17072 KB |
Output is correct |
5 |
Correct |
15 ms |
17136 KB |
Output is correct |
6 |
Correct |
16 ms |
17136 KB |
Output is correct |
7 |
Correct |
15 ms |
17136 KB |
Output is correct |
8 |
Correct |
16 ms |
17136 KB |
Output is correct |
9 |
Correct |
15 ms |
17136 KB |
Output is correct |
10 |
Correct |
16 ms |
17136 KB |
Output is correct |
11 |
Correct |
15 ms |
17136 KB |
Output is correct |
12 |
Correct |
19 ms |
17136 KB |
Output is correct |
13 |
Correct |
15 ms |
17136 KB |
Output is correct |
14 |
Correct |
19 ms |
17136 KB |
Output is correct |
15 |
Correct |
16 ms |
17136 KB |
Output is correct |
16 |
Correct |
15 ms |
17136 KB |
Output is correct |
17 |
Correct |
15 ms |
17136 KB |
Output is correct |
18 |
Correct |
17 ms |
17136 KB |
Output is correct |
19 |
Correct |
21 ms |
17136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16872 KB |
Output is correct |
3 |
Correct |
16 ms |
16948 KB |
Output is correct |
4 |
Correct |
17 ms |
16948 KB |
Output is correct |
5 |
Correct |
18 ms |
17048 KB |
Output is correct |
6 |
Correct |
18 ms |
17048 KB |
Output is correct |
7 |
Correct |
16 ms |
17048 KB |
Output is correct |
8 |
Correct |
21 ms |
17048 KB |
Output is correct |
9 |
Correct |
16 ms |
17048 KB |
Output is correct |
10 |
Correct |
16 ms |
17048 KB |
Output is correct |
11 |
Correct |
17 ms |
17048 KB |
Output is correct |
12 |
Correct |
17 ms |
17048 KB |
Output is correct |
13 |
Correct |
15 ms |
17048 KB |
Output is correct |
14 |
Correct |
18 ms |
17072 KB |
Output is correct |
15 |
Correct |
15 ms |
17136 KB |
Output is correct |
16 |
Correct |
16 ms |
17136 KB |
Output is correct |
17 |
Correct |
15 ms |
17136 KB |
Output is correct |
18 |
Correct |
16 ms |
17136 KB |
Output is correct |
19 |
Correct |
15 ms |
17136 KB |
Output is correct |
20 |
Correct |
16 ms |
17136 KB |
Output is correct |
21 |
Correct |
15 ms |
17136 KB |
Output is correct |
22 |
Correct |
19 ms |
17136 KB |
Output is correct |
23 |
Correct |
15 ms |
17136 KB |
Output is correct |
24 |
Correct |
19 ms |
17136 KB |
Output is correct |
25 |
Correct |
16 ms |
17136 KB |
Output is correct |
26 |
Correct |
15 ms |
17136 KB |
Output is correct |
27 |
Correct |
15 ms |
17136 KB |
Output is correct |
28 |
Correct |
17 ms |
17136 KB |
Output is correct |
29 |
Correct |
21 ms |
17136 KB |
Output is correct |
30 |
Correct |
16 ms |
17148 KB |
Output is correct |
31 |
Correct |
15 ms |
17148 KB |
Output is correct |
32 |
Correct |
16 ms |
17148 KB |
Output is correct |
33 |
Correct |
16 ms |
17148 KB |
Output is correct |
34 |
Correct |
17 ms |
17276 KB |
Output is correct |
35 |
Correct |
16 ms |
17276 KB |
Output is correct |
36 |
Correct |
17 ms |
17276 KB |
Output is correct |
37 |
Correct |
17 ms |
17404 KB |
Output is correct |
38 |
Correct |
19 ms |
17404 KB |
Output is correct |
39 |
Correct |
17 ms |
17404 KB |
Output is correct |
40 |
Correct |
17 ms |
17404 KB |
Output is correct |
41 |
Correct |
16 ms |
17404 KB |
Output is correct |
42 |
Correct |
19 ms |
17404 KB |
Output is correct |
43 |
Correct |
21 ms |
17404 KB |
Output is correct |
44 |
Correct |
18 ms |
17512 KB |
Output is correct |
45 |
Correct |
18 ms |
17512 KB |
Output is correct |
46 |
Correct |
17 ms |
17532 KB |
Output is correct |
47 |
Correct |
24 ms |
17532 KB |
Output is correct |
48 |
Correct |
22 ms |
17532 KB |
Output is correct |
49 |
Correct |
17 ms |
17532 KB |
Output is correct |
50 |
Correct |
18 ms |
17532 KB |
Output is correct |
51 |
Correct |
17 ms |
17532 KB |
Output is correct |
52 |
Correct |
18 ms |
17532 KB |
Output is correct |
53 |
Correct |
21 ms |
17532 KB |
Output is correct |
54 |
Correct |
21 ms |
17564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16872 KB |
Output is correct |
3 |
Correct |
16 ms |
16948 KB |
Output is correct |
4 |
Correct |
17 ms |
16948 KB |
Output is correct |
5 |
Correct |
18 ms |
17048 KB |
Output is correct |
6 |
Correct |
18 ms |
17048 KB |
Output is correct |
7 |
Correct |
16 ms |
17048 KB |
Output is correct |
8 |
Correct |
21 ms |
17048 KB |
Output is correct |
9 |
Correct |
16 ms |
17048 KB |
Output is correct |
10 |
Correct |
16 ms |
17048 KB |
Output is correct |
11 |
Correct |
17 ms |
17048 KB |
Output is correct |
12 |
Correct |
17 ms |
17048 KB |
Output is correct |
13 |
Correct |
15 ms |
17048 KB |
Output is correct |
14 |
Correct |
18 ms |
17072 KB |
Output is correct |
15 |
Correct |
15 ms |
17136 KB |
Output is correct |
16 |
Correct |
16 ms |
17136 KB |
Output is correct |
17 |
Correct |
15 ms |
17136 KB |
Output is correct |
18 |
Correct |
16 ms |
17136 KB |
Output is correct |
19 |
Correct |
15 ms |
17136 KB |
Output is correct |
20 |
Correct |
16 ms |
17136 KB |
Output is correct |
21 |
Correct |
15 ms |
17136 KB |
Output is correct |
22 |
Correct |
19 ms |
17136 KB |
Output is correct |
23 |
Correct |
15 ms |
17136 KB |
Output is correct |
24 |
Correct |
19 ms |
17136 KB |
Output is correct |
25 |
Correct |
16 ms |
17136 KB |
Output is correct |
26 |
Correct |
15 ms |
17136 KB |
Output is correct |
27 |
Correct |
15 ms |
17136 KB |
Output is correct |
28 |
Correct |
17 ms |
17136 KB |
Output is correct |
29 |
Correct |
21 ms |
17136 KB |
Output is correct |
30 |
Correct |
16 ms |
17148 KB |
Output is correct |
31 |
Correct |
15 ms |
17148 KB |
Output is correct |
32 |
Correct |
16 ms |
17148 KB |
Output is correct |
33 |
Correct |
16 ms |
17148 KB |
Output is correct |
34 |
Correct |
17 ms |
17276 KB |
Output is correct |
35 |
Correct |
16 ms |
17276 KB |
Output is correct |
36 |
Correct |
17 ms |
17276 KB |
Output is correct |
37 |
Correct |
17 ms |
17404 KB |
Output is correct |
38 |
Correct |
19 ms |
17404 KB |
Output is correct |
39 |
Correct |
17 ms |
17404 KB |
Output is correct |
40 |
Correct |
17 ms |
17404 KB |
Output is correct |
41 |
Correct |
16 ms |
17404 KB |
Output is correct |
42 |
Correct |
19 ms |
17404 KB |
Output is correct |
43 |
Correct |
21 ms |
17404 KB |
Output is correct |
44 |
Correct |
18 ms |
17512 KB |
Output is correct |
45 |
Correct |
18 ms |
17512 KB |
Output is correct |
46 |
Correct |
17 ms |
17532 KB |
Output is correct |
47 |
Correct |
24 ms |
17532 KB |
Output is correct |
48 |
Correct |
22 ms |
17532 KB |
Output is correct |
49 |
Correct |
17 ms |
17532 KB |
Output is correct |
50 |
Correct |
18 ms |
17532 KB |
Output is correct |
51 |
Correct |
17 ms |
17532 KB |
Output is correct |
52 |
Correct |
18 ms |
17532 KB |
Output is correct |
53 |
Correct |
21 ms |
17532 KB |
Output is correct |
54 |
Correct |
21 ms |
17564 KB |
Output is correct |
55 |
Correct |
20 ms |
18044 KB |
Output is correct |
56 |
Correct |
39 ms |
21116 KB |
Output is correct |
57 |
Correct |
64 ms |
23756 KB |
Output is correct |
58 |
Correct |
80 ms |
25612 KB |
Output is correct |
59 |
Correct |
111 ms |
28408 KB |
Output is correct |
60 |
Correct |
161 ms |
31100 KB |
Output is correct |
61 |
Correct |
189 ms |
33436 KB |
Output is correct |
62 |
Correct |
231 ms |
34788 KB |
Output is correct |
63 |
Correct |
264 ms |
38384 KB |
Output is correct |
64 |
Correct |
265 ms |
39152 KB |
Output is correct |
65 |
Correct |
68 ms |
39152 KB |
Output is correct |
66 |
Correct |
65 ms |
39152 KB |
Output is correct |
67 |
Correct |
64 ms |
39152 KB |
Output is correct |
68 |
Correct |
151 ms |
39152 KB |
Output is correct |
69 |
Correct |
198 ms |
39152 KB |
Output is correct |
70 |
Correct |
201 ms |
39152 KB |
Output is correct |
71 |
Correct |
275 ms |
48812 KB |
Output is correct |
72 |
Correct |
269 ms |
48968 KB |
Output is correct |
73 |
Correct |
201 ms |
48968 KB |
Output is correct |
74 |
Correct |
204 ms |
48968 KB |
Output is correct |
75 |
Correct |
229 ms |
48968 KB |
Output is correct |
76 |
Correct |
208 ms |
48968 KB |
Output is correct |
77 |
Correct |
213 ms |
48968 KB |
Output is correct |
78 |
Correct |
226 ms |
48968 KB |
Output is correct |
79 |
Correct |
207 ms |
48968 KB |
Output is correct |