#pragma GCC optimize("O3,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();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7316 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
5 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7304 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 |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
5 ms |
7392 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7316 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
5 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 |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7304 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
4 ms |
7252 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
5 ms |
7392 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 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 |
6 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7384 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7452 KB |
Output is correct |
34 |
Correct |
5 ms |
7508 KB |
Output is correct |
35 |
Correct |
6 ms |
7544 KB |
Output is correct |
36 |
Correct |
6 ms |
7508 KB |
Output is correct |
37 |
Correct |
6 ms |
7540 KB |
Output is correct |
38 |
Correct |
9 ms |
7508 KB |
Output is correct |
39 |
Correct |
7 ms |
7508 KB |
Output is correct |
40 |
Correct |
6 ms |
8080 KB |
Output is correct |
41 |
Correct |
6 ms |
8020 KB |
Output is correct |
42 |
Correct |
5 ms |
7508 KB |
Output is correct |
43 |
Correct |
6 ms |
7892 KB |
Output is correct |
44 |
Correct |
7 ms |
7788 KB |
Output is correct |
45 |
Correct |
6 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 |
6 ms |
7764 KB |
Output is correct |
49 |
Correct |
7 ms |
7752 KB |
Output is correct |
50 |
Correct |
6 ms |
7764 KB |
Output is correct |
51 |
Correct |
6 ms |
7636 KB |
Output is correct |
52 |
Correct |
6 ms |
7636 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
7 ms |
7764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7316 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
5 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 |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7304 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
4 ms |
7252 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
5 ms |
7392 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 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 |
6 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7384 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7452 KB |
Output is correct |
34 |
Correct |
5 ms |
7508 KB |
Output is correct |
35 |
Correct |
6 ms |
7544 KB |
Output is correct |
36 |
Correct |
6 ms |
7508 KB |
Output is correct |
37 |
Correct |
6 ms |
7540 KB |
Output is correct |
38 |
Correct |
9 ms |
7508 KB |
Output is correct |
39 |
Correct |
7 ms |
7508 KB |
Output is correct |
40 |
Correct |
6 ms |
8080 KB |
Output is correct |
41 |
Correct |
6 ms |
8020 KB |
Output is correct |
42 |
Correct |
5 ms |
7508 KB |
Output is correct |
43 |
Correct |
6 ms |
7892 KB |
Output is correct |
44 |
Correct |
7 ms |
7788 KB |
Output is correct |
45 |
Correct |
6 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 |
6 ms |
7764 KB |
Output is correct |
49 |
Correct |
7 ms |
7752 KB |
Output is correct |
50 |
Correct |
6 ms |
7764 KB |
Output is correct |
51 |
Correct |
6 ms |
7636 KB |
Output is correct |
52 |
Correct |
6 ms |
7636 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
7 ms |
7764 KB |
Output is correct |
55 |
Correct |
10 ms |
8020 KB |
Output is correct |
56 |
Correct |
30 ms |
9816 KB |
Output is correct |
57 |
Correct |
50 ms |
10956 KB |
Output is correct |
58 |
Correct |
61 ms |
12364 KB |
Output is correct |
59 |
Correct |
84 ms |
13664 KB |
Output is correct |
60 |
Correct |
107 ms |
15368 KB |
Output is correct |
61 |
Correct |
155 ms |
17032 KB |
Output is correct |
62 |
Correct |
138 ms |
17760 KB |
Output is correct |
63 |
Correct |
170 ms |
19096 KB |
Output is correct |
64 |
Correct |
178 ms |
19712 KB |
Output is correct |
65 |
Correct |
93 ms |
54196 KB |
Output is correct |
66 |
Correct |
96 ms |
54264 KB |
Output is correct |
67 |
Correct |
87 ms |
16844 KB |
Output is correct |
68 |
Correct |
165 ms |
41628 KB |
Output is correct |
69 |
Correct |
180 ms |
39216 KB |
Output is correct |
70 |
Correct |
185 ms |
39216 KB |
Output is correct |
71 |
Correct |
218 ms |
31244 KB |
Output is correct |
72 |
Correct |
214 ms |
31308 KB |
Output is correct |
73 |
Correct |
195 ms |
30492 KB |
Output is correct |
74 |
Correct |
192 ms |
30476 KB |
Output is correct |
75 |
Correct |
191 ms |
29720 KB |
Output is correct |
76 |
Correct |
197 ms |
29700 KB |
Output is correct |
77 |
Correct |
199 ms |
29740 KB |
Output is correct |
78 |
Correct |
198 ms |
29676 KB |
Output is correct |
79 |
Correct |
195 ms |
31320 KB |
Output is correct |