#pragma GCC optimize("Ofast,unroll-loops")
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=3e5+7;
vector<pair<int,ll>>V[LIM];
ll n, m, ans;
void DFS2(int x, ll k, priority_queue<ll>&q) {
ans+=k;
if(x>=n) {
q.push(0);
q.push(k);
q.push(k);
return;
}
DFS2(V[x][0].st, V[x][0].nd, q);
for(int i=1; i<V[x].size(); ++i) {
priority_queue<ll>p;
DFS2(V[x][i].st, V[x][i].nd, p);
if(p.size()>q.size()) swap(p, q);
while(!p.empty()) {
q.push(p.top());
p.pop();
}
}
rep(i, V[x].size()-1) q.pop();
ll a=q.top(); q.pop();
ll b=q.top(); q.pop();
q.push(a+k); q.push(b+k);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i, n+m-1) {
ll a, b;
cin >> a >> b; --a;
V[a].pb({i+1, b});
}
priority_queue<ll>Q;
DFS2(0, 0, Q);
Q.pop();
vector<ll>P;
while(!Q.empty()) {
P.pb(Q.top());
Q.pop();
}
for(ll i=P.size()-1; i; --i) ans-=i*(P[i-1]-P[i]);
cout << ans << '\n';
}
Compilation message
fireworks.cpp: In function 'void DFS2(int, ll, std::priority_queue<long long int>&)':
fireworks.cpp:26:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i=1; i<V[x].size(); ++i) {
| ~^~~~~~~~~~~~
fireworks.cpp:9:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
fireworks.cpp:35:13: note: in expansion of macro 'rep'
35 | rep(i, V[x].size()-1) q.pop();
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
5 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
5 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7316 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7252 KB |
Output is correct |
15 |
Correct |
5 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
5 ms |
7252 KB |
Output is correct |
18 |
Correct |
5 ms |
7252 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7316 KB |
Output is correct |
23 |
Correct |
5 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
6 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
5 ms |
7380 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
6 ms |
7508 KB |
Output is correct |
35 |
Correct |
6 ms |
7508 KB |
Output is correct |
36 |
Correct |
6 ms |
7508 KB |
Output is correct |
37 |
Correct |
6 ms |
7592 KB |
Output is correct |
38 |
Correct |
6 ms |
7508 KB |
Output is correct |
39 |
Correct |
7 ms |
7508 KB |
Output is correct |
40 |
Correct |
5 ms |
8032 KB |
Output is correct |
41 |
Correct |
6 ms |
8148 KB |
Output is correct |
42 |
Correct |
6 ms |
7508 KB |
Output is correct |
43 |
Correct |
6 ms |
7892 KB |
Output is correct |
44 |
Correct |
7 ms |
7892 KB |
Output is correct |
45 |
Correct |
7 ms |
7848 KB |
Output is correct |
46 |
Correct |
7 ms |
7764 KB |
Output is correct |
47 |
Correct |
7 ms |
7764 KB |
Output is correct |
48 |
Correct |
7 ms |
7764 KB |
Output is correct |
49 |
Correct |
6 ms |
7764 KB |
Output is correct |
50 |
Correct |
6 ms |
7764 KB |
Output is correct |
51 |
Correct |
6 ms |
7636 KB |
Output is correct |
52 |
Correct |
7 ms |
7700 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
7 ms |
7764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7252 KB |
Output is correct |
15 |
Correct |
5 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
5 ms |
7252 KB |
Output is correct |
18 |
Correct |
5 ms |
7252 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7316 KB |
Output is correct |
23 |
Correct |
5 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
6 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
5 ms |
7380 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
6 ms |
7508 KB |
Output is correct |
35 |
Correct |
6 ms |
7508 KB |
Output is correct |
36 |
Correct |
6 ms |
7508 KB |
Output is correct |
37 |
Correct |
6 ms |
7592 KB |
Output is correct |
38 |
Correct |
6 ms |
7508 KB |
Output is correct |
39 |
Correct |
7 ms |
7508 KB |
Output is correct |
40 |
Correct |
5 ms |
8032 KB |
Output is correct |
41 |
Correct |
6 ms |
8148 KB |
Output is correct |
42 |
Correct |
6 ms |
7508 KB |
Output is correct |
43 |
Correct |
6 ms |
7892 KB |
Output is correct |
44 |
Correct |
7 ms |
7892 KB |
Output is correct |
45 |
Correct |
7 ms |
7848 KB |
Output is correct |
46 |
Correct |
7 ms |
7764 KB |
Output is correct |
47 |
Correct |
7 ms |
7764 KB |
Output is correct |
48 |
Correct |
7 ms |
7764 KB |
Output is correct |
49 |
Correct |
6 ms |
7764 KB |
Output is correct |
50 |
Correct |
6 ms |
7764 KB |
Output is correct |
51 |
Correct |
6 ms |
7636 KB |
Output is correct |
52 |
Correct |
7 ms |
7700 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
7 ms |
7764 KB |
Output is correct |
55 |
Correct |
14 ms |
8000 KB |
Output is correct |
56 |
Correct |
28 ms |
9752 KB |
Output is correct |
57 |
Correct |
46 ms |
10940 KB |
Output is correct |
58 |
Correct |
58 ms |
12392 KB |
Output is correct |
59 |
Correct |
98 ms |
13608 KB |
Output is correct |
60 |
Correct |
120 ms |
15324 KB |
Output is correct |
61 |
Correct |
120 ms |
17108 KB |
Output is correct |
62 |
Correct |
141 ms |
17788 KB |
Output is correct |
63 |
Correct |
184 ms |
19256 KB |
Output is correct |
64 |
Correct |
182 ms |
19648 KB |
Output is correct |
65 |
Correct |
98 ms |
54220 KB |
Output is correct |
66 |
Correct |
96 ms |
54264 KB |
Output is correct |
67 |
Correct |
86 ms |
16784 KB |
Output is correct |
68 |
Correct |
185 ms |
41648 KB |
Output is correct |
69 |
Correct |
181 ms |
39216 KB |
Output is correct |
70 |
Correct |
179 ms |
39232 KB |
Output is correct |
71 |
Correct |
234 ms |
31296 KB |
Output is correct |
72 |
Correct |
217 ms |
31228 KB |
Output is correct |
73 |
Correct |
199 ms |
30336 KB |
Output is correct |
74 |
Correct |
208 ms |
30860 KB |
Output is correct |
75 |
Correct |
204 ms |
29724 KB |
Output is correct |
76 |
Correct |
202 ms |
29772 KB |
Output is correct |
77 |
Correct |
198 ms |
29796 KB |
Output is correct |
78 |
Correct |
196 ms |
29676 KB |
Output is correct |
79 |
Correct |
225 ms |
31412 KB |
Output is correct |