///
/// ♪ Pizza mozzarella rella rella rella... ♪
///
#define _USE_MATH_DEFINES
#define FAST ios::sync_with_stdio(false),cin.tie(0);
#include <bits/stdc++.h>
#define Loop(x, l, r) for(int x = (l); x < (r); ++x)
#define LoopR(x, l, r) for(int x = (r)-1; x >= (l); --x)
#define all(x) x.begin(), x.end()
#define Kill(x) exit((cout << (x) << '\n', 0))
#define YN(flag) ((flag)? "YES": "NO")
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
typedef std::pair<ll ,ll > pll;
using namespace std;
const int N = 300'010;
vector<pll> A[N];
int n, m;
struct obj
{
priority_queue<ll, vector<ll>, less<ll> > l;
priority_queue<ll, vector<ll>, greater<ll>> r;
ll w=0;
void add(ll x)
{
if(!l.size()) l.push(0);
if(!r.size()) r.push(1e15);
ll nl = l.top()+x;
ll nr = r.top()+x;
l.pop(); l.push(nl);
while(r.size()) r.pop(); r.push(nr);
}
void add(obj& x)
{
w += x.w;
if(l.size() < x.l.size()) l.swap(x.l);
if(r.size() < x.r.size()) r.swap(x.r);
while(x.l.size()) l.push(x.l.top()), x.l.pop();
while(x.r.size()) r.push(x.r.top()), x.r.pop();
while(l.size() && r.size() && l.top() > r.top())
{
auto _l = l.top(), _r = r.top();
l.pop(); r.pop();
l.push(_r); r.push(_l);
w += _l - _r;
}
}
};
obj* dfs(int v)
{
obj* ans = new obj;
if(v >= n) {ans->r.push(0); return ans;}
for(auto [u, w] : A[v])
{
auto x = dfs(u);
x->add(w);
ans->add(*x);
}
return ans;
}
int main()
{
FAST;
cin >> n >> m;
Loop(i,1,n+m)
{
ll v, c;
cin >> v >> c;
--v;
A[v].emplace_back(i,c);
}
cout << dfs(0)->w;
}
Compilation message
fireworks.cpp: In member function 'void obj::add(ll)':
fireworks.cpp:38:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
38 | while(r.size()) r.pop(); r.push(nr);
| ^~~~~
fireworks.cpp:38:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
38 | while(r.size()) r.pop(); r.push(nr);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7360 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7288 KB |
Output is correct |
7 |
Correct |
6 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
6 ms |
7372 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7360 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7288 KB |
Output is correct |
7 |
Correct |
5 ms |
7372 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
5 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7360 KB |
Output is correct |
13 |
Correct |
5 ms |
7372 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7372 KB |
Output is correct |
19 |
Correct |
5 ms |
7356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7360 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7288 KB |
Output is correct |
7 |
Correct |
6 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
6 ms |
7372 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7360 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7288 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 ms |
7372 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
5 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7360 KB |
Output is correct |
23 |
Correct |
5 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
5 ms |
7372 KB |
Output is correct |
26 |
Correct |
4 ms |
7372 KB |
Output is correct |
27 |
Correct |
5 ms |
7372 KB |
Output is correct |
28 |
Correct |
4 ms |
7372 KB |
Output is correct |
29 |
Correct |
5 ms |
7356 KB |
Output is correct |
30 |
Correct |
5 ms |
7388 KB |
Output is correct |
31 |
Correct |
5 ms |
7500 KB |
Output is correct |
32 |
Correct |
5 ms |
7552 KB |
Output is correct |
33 |
Correct |
6 ms |
7628 KB |
Output is correct |
34 |
Correct |
6 ms |
7756 KB |
Output is correct |
35 |
Correct |
8 ms |
7884 KB |
Output is correct |
36 |
Correct |
8 ms |
7884 KB |
Output is correct |
37 |
Correct |
9 ms |
8012 KB |
Output is correct |
38 |
Correct |
7 ms |
8012 KB |
Output is correct |
39 |
Correct |
7 ms |
8140 KB |
Output is correct |
40 |
Correct |
6 ms |
8396 KB |
Output is correct |
41 |
Correct |
6 ms |
8396 KB |
Output is correct |
42 |
Correct |
6 ms |
7884 KB |
Output is correct |
43 |
Correct |
7 ms |
8268 KB |
Output is correct |
44 |
Correct |
8 ms |
8296 KB |
Output is correct |
45 |
Correct |
7 ms |
8268 KB |
Output is correct |
46 |
Correct |
9 ms |
8268 KB |
Output is correct |
47 |
Correct |
10 ms |
8284 KB |
Output is correct |
48 |
Correct |
8 ms |
8168 KB |
Output is correct |
49 |
Correct |
8 ms |
8332 KB |
Output is correct |
50 |
Correct |
7 ms |
8140 KB |
Output is correct |
51 |
Correct |
7 ms |
8096 KB |
Output is correct |
52 |
Correct |
8 ms |
8224 KB |
Output is correct |
53 |
Correct |
8 ms |
8268 KB |
Output is correct |
54 |
Correct |
9 ms |
8264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7360 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7288 KB |
Output is correct |
7 |
Correct |
6 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
6 ms |
7372 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7360 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7288 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 ms |
7372 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
5 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7360 KB |
Output is correct |
23 |
Correct |
5 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
5 ms |
7372 KB |
Output is correct |
26 |
Correct |
4 ms |
7372 KB |
Output is correct |
27 |
Correct |
5 ms |
7372 KB |
Output is correct |
28 |
Correct |
4 ms |
7372 KB |
Output is correct |
29 |
Correct |
5 ms |
7356 KB |
Output is correct |
30 |
Correct |
5 ms |
7388 KB |
Output is correct |
31 |
Correct |
5 ms |
7500 KB |
Output is correct |
32 |
Correct |
5 ms |
7552 KB |
Output is correct |
33 |
Correct |
6 ms |
7628 KB |
Output is correct |
34 |
Correct |
6 ms |
7756 KB |
Output is correct |
35 |
Correct |
8 ms |
7884 KB |
Output is correct |
36 |
Correct |
8 ms |
7884 KB |
Output is correct |
37 |
Correct |
9 ms |
8012 KB |
Output is correct |
38 |
Correct |
7 ms |
8012 KB |
Output is correct |
39 |
Correct |
7 ms |
8140 KB |
Output is correct |
40 |
Correct |
6 ms |
8396 KB |
Output is correct |
41 |
Correct |
6 ms |
8396 KB |
Output is correct |
42 |
Correct |
6 ms |
7884 KB |
Output is correct |
43 |
Correct |
7 ms |
8268 KB |
Output is correct |
44 |
Correct |
8 ms |
8296 KB |
Output is correct |
45 |
Correct |
7 ms |
8268 KB |
Output is correct |
46 |
Correct |
9 ms |
8268 KB |
Output is correct |
47 |
Correct |
10 ms |
8284 KB |
Output is correct |
48 |
Correct |
8 ms |
8168 KB |
Output is correct |
49 |
Correct |
8 ms |
8332 KB |
Output is correct |
50 |
Correct |
7 ms |
8140 KB |
Output is correct |
51 |
Correct |
7 ms |
8096 KB |
Output is correct |
52 |
Correct |
8 ms |
8224 KB |
Output is correct |
53 |
Correct |
8 ms |
8268 KB |
Output is correct |
54 |
Correct |
9 ms |
8264 KB |
Output is correct |
55 |
Correct |
13 ms |
9420 KB |
Output is correct |
56 |
Correct |
36 ms |
14964 KB |
Output is correct |
57 |
Correct |
64 ms |
20572 KB |
Output is correct |
58 |
Correct |
78 ms |
24148 KB |
Output is correct |
59 |
Correct |
110 ms |
29784 KB |
Output is correct |
60 |
Correct |
141 ms |
35464 KB |
Output is correct |
61 |
Correct |
162 ms |
39176 KB |
Output is correct |
62 |
Correct |
175 ms |
42720 KB |
Output is correct |
63 |
Correct |
207 ms |
49648 KB |
Output is correct |
64 |
Correct |
222 ms |
51896 KB |
Output is correct |
65 |
Correct |
136 ms |
73156 KB |
Output is correct |
66 |
Correct |
142 ms |
73364 KB |
Output is correct |
67 |
Correct |
135 ms |
45252 KB |
Output is correct |
68 |
Correct |
182 ms |
67248 KB |
Output is correct |
69 |
Correct |
211 ms |
65544 KB |
Output is correct |
70 |
Correct |
208 ms |
65444 KB |
Output is correct |
71 |
Correct |
265 ms |
71856 KB |
Output is correct |
72 |
Correct |
261 ms |
71852 KB |
Output is correct |
73 |
Correct |
241 ms |
68140 KB |
Output is correct |
74 |
Correct |
240 ms |
68404 KB |
Output is correct |
75 |
Correct |
246 ms |
66484 KB |
Output is correct |
76 |
Correct |
246 ms |
66604 KB |
Output is correct |
77 |
Correct |
265 ms |
64368 KB |
Output is correct |
78 |
Correct |
264 ms |
63592 KB |
Output is correct |
79 |
Correct |
205 ms |
62500 KB |
Output is correct |