#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
const int MAXN = 300005;
const llint INF = 1000000000000000LL;
int n, m;
llint p[MAXN], c[MAXN];
llint a[MAXN], b[MAXN];
vector <int> v[MAXN];
priority_queue <llint> hull[MAXN];
void add (int x, int len) {
int slope = a[x] + hull[x].size();
llint x1, x2;
while (slope >= 0) {
if (slope == 1) x2 = hull[x].top();
if (slope == 0) x1 = hull[x].top();
hull[x].pop(); slope--;
}
b[x] += len;
hull[x].push(x1 + len);
hull[x].push(x2 + len);
}
void spoji (int x, int y) {
if (hull[x].size() > hull[y].size()) swap(hull[x], hull[y]);
a[y] += a[x];
b[y] += b[x];
while (!hull[x].empty()) {
hull[y].push(hull[x].top());
hull[x].pop();
}
}
void dfs (int x) {
if (x >= n + 1) {
a[x] = -1; b[x] = 0;
hull[x].push(0);
hull[x].push(0);
return;
}
for (int i=0; i<v[x].size(); i++) {
int sus = v[x][i];
dfs(sus);
add(sus, c[sus]);
spoji(sus, x);
}
}
llint upit (int x) {
vector <llint> r;
while (!hull[x].empty()) {
r.push_back(hull[x].top());
hull[x].pop();
}
llint val = b[x], slope = a[x], pos = 0;
llint sol = val;
while (!r.empty()) {
val += slope * (r.back() - pos);
sol = min(sol, val);
pos = r.back();
r.pop_back();
slope++;
}
return sol;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 2; i <= n+m; i++) {
cin >> p[i] >> c[i];
v[p[i]].push_back(i);
}
dfs(1);
cout << upit(1);
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
fireworks.cpp: In function 'void add(int, int)':
fireworks.cpp:26:21: warning: 'x2' may be used uninitialized in this function [-Wmaybe-uninitialized]
hull[x].push(x2 + len);
~~~^~~~~
fireworks.cpp:25:21: warning: 'x1' may be used uninitialized in this function [-Wmaybe-uninitialized]
hull[x].push(x1 + len);
~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
16 ms |
17016 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16888 KB |
Output is correct |
6 |
Correct |
16 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
16 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16896 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
15 ms |
16768 KB |
Output is correct |
6 |
Correct |
15 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
15 ms |
16896 KB |
Output is correct |
11 |
Correct |
15 ms |
16868 KB |
Output is correct |
12 |
Correct |
16 ms |
16896 KB |
Output is correct |
13 |
Correct |
15 ms |
16768 KB |
Output is correct |
14 |
Correct |
15 ms |
16768 KB |
Output is correct |
15 |
Correct |
17 ms |
16896 KB |
Output is correct |
16 |
Correct |
17 ms |
17024 KB |
Output is correct |
17 |
Correct |
15 ms |
16896 KB |
Output is correct |
18 |
Correct |
17 ms |
16896 KB |
Output is correct |
19 |
Correct |
15 ms |
16896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
16 ms |
17016 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16888 KB |
Output is correct |
6 |
Correct |
16 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
16 ms |
16768 KB |
Output is correct |
11 |
Correct |
16 ms |
16896 KB |
Output is correct |
12 |
Correct |
15 ms |
16768 KB |
Output is correct |
13 |
Correct |
15 ms |
16768 KB |
Output is correct |
14 |
Correct |
16 ms |
16896 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
15 ms |
16768 KB |
Output is correct |
17 |
Correct |
15 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
16 ms |
16768 KB |
Output is correct |
20 |
Correct |
15 ms |
16896 KB |
Output is correct |
21 |
Correct |
15 ms |
16868 KB |
Output is correct |
22 |
Correct |
16 ms |
16896 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
15 ms |
16768 KB |
Output is correct |
25 |
Correct |
17 ms |
16896 KB |
Output is correct |
26 |
Correct |
17 ms |
17024 KB |
Output is correct |
27 |
Correct |
15 ms |
16896 KB |
Output is correct |
28 |
Correct |
17 ms |
16896 KB |
Output is correct |
29 |
Correct |
15 ms |
16896 KB |
Output is correct |
30 |
Correct |
15 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16896 KB |
Output is correct |
32 |
Correct |
17 ms |
16896 KB |
Output is correct |
33 |
Correct |
18 ms |
17024 KB |
Output is correct |
34 |
Correct |
18 ms |
17024 KB |
Output is correct |
35 |
Correct |
18 ms |
17124 KB |
Output is correct |
36 |
Correct |
18 ms |
17152 KB |
Output is correct |
37 |
Correct |
18 ms |
17280 KB |
Output is correct |
38 |
Correct |
19 ms |
17280 KB |
Output is correct |
39 |
Correct |
19 ms |
17256 KB |
Output is correct |
40 |
Correct |
19 ms |
17664 KB |
Output is correct |
41 |
Correct |
18 ms |
17664 KB |
Output is correct |
42 |
Correct |
17 ms |
17152 KB |
Output is correct |
43 |
Correct |
20 ms |
17436 KB |
Output is correct |
44 |
Correct |
20 ms |
17408 KB |
Output is correct |
45 |
Correct |
19 ms |
17408 KB |
Output is correct |
46 |
Correct |
19 ms |
17528 KB |
Output is correct |
47 |
Correct |
19 ms |
17408 KB |
Output is correct |
48 |
Correct |
19 ms |
17408 KB |
Output is correct |
49 |
Correct |
20 ms |
17472 KB |
Output is correct |
50 |
Correct |
19 ms |
17280 KB |
Output is correct |
51 |
Correct |
18 ms |
17280 KB |
Output is correct |
52 |
Correct |
18 ms |
17408 KB |
Output is correct |
53 |
Correct |
20 ms |
17408 KB |
Output is correct |
54 |
Correct |
19 ms |
17504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
16 ms |
17016 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16888 KB |
Output is correct |
6 |
Correct |
16 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
16 ms |
16768 KB |
Output is correct |
11 |
Correct |
16 ms |
16896 KB |
Output is correct |
12 |
Correct |
15 ms |
16768 KB |
Output is correct |
13 |
Correct |
15 ms |
16768 KB |
Output is correct |
14 |
Correct |
16 ms |
16896 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
15 ms |
16768 KB |
Output is correct |
17 |
Correct |
15 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
16 ms |
16768 KB |
Output is correct |
20 |
Correct |
15 ms |
16896 KB |
Output is correct |
21 |
Correct |
15 ms |
16868 KB |
Output is correct |
22 |
Correct |
16 ms |
16896 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
15 ms |
16768 KB |
Output is correct |
25 |
Correct |
17 ms |
16896 KB |
Output is correct |
26 |
Correct |
17 ms |
17024 KB |
Output is correct |
27 |
Correct |
15 ms |
16896 KB |
Output is correct |
28 |
Correct |
17 ms |
16896 KB |
Output is correct |
29 |
Correct |
15 ms |
16896 KB |
Output is correct |
30 |
Correct |
15 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16896 KB |
Output is correct |
32 |
Correct |
17 ms |
16896 KB |
Output is correct |
33 |
Correct |
18 ms |
17024 KB |
Output is correct |
34 |
Correct |
18 ms |
17024 KB |
Output is correct |
35 |
Correct |
18 ms |
17124 KB |
Output is correct |
36 |
Correct |
18 ms |
17152 KB |
Output is correct |
37 |
Correct |
18 ms |
17280 KB |
Output is correct |
38 |
Correct |
19 ms |
17280 KB |
Output is correct |
39 |
Correct |
19 ms |
17256 KB |
Output is correct |
40 |
Correct |
19 ms |
17664 KB |
Output is correct |
41 |
Correct |
18 ms |
17664 KB |
Output is correct |
42 |
Correct |
17 ms |
17152 KB |
Output is correct |
43 |
Correct |
20 ms |
17436 KB |
Output is correct |
44 |
Correct |
20 ms |
17408 KB |
Output is correct |
45 |
Correct |
19 ms |
17408 KB |
Output is correct |
46 |
Correct |
19 ms |
17528 KB |
Output is correct |
47 |
Correct |
19 ms |
17408 KB |
Output is correct |
48 |
Correct |
19 ms |
17408 KB |
Output is correct |
49 |
Correct |
20 ms |
17472 KB |
Output is correct |
50 |
Correct |
19 ms |
17280 KB |
Output is correct |
51 |
Correct |
18 ms |
17280 KB |
Output is correct |
52 |
Correct |
18 ms |
17408 KB |
Output is correct |
53 |
Correct |
20 ms |
17408 KB |
Output is correct |
54 |
Correct |
19 ms |
17504 KB |
Output is correct |
55 |
Correct |
25 ms |
18048 KB |
Output is correct |
56 |
Correct |
55 ms |
21748 KB |
Output is correct |
57 |
Correct |
90 ms |
25028 KB |
Output is correct |
58 |
Correct |
108 ms |
27632 KB |
Output is correct |
59 |
Correct |
151 ms |
30960 KB |
Output is correct |
60 |
Correct |
185 ms |
34288 KB |
Output is correct |
61 |
Correct |
231 ms |
37580 KB |
Output is correct |
62 |
Correct |
258 ms |
39372 KB |
Output is correct |
63 |
Correct |
318 ms |
43628 KB |
Output is correct |
64 |
Correct |
345 ms |
44904 KB |
Output is correct |
65 |
Correct |
138 ms |
68600 KB |
Output is correct |
66 |
Correct |
133 ms |
68600 KB |
Output is correct |
67 |
Correct |
180 ms |
40532 KB |
Output is correct |
68 |
Correct |
205 ms |
58720 KB |
Output is correct |
69 |
Correct |
266 ms |
56264 KB |
Output is correct |
70 |
Correct |
280 ms |
56560 KB |
Output is correct |
71 |
Correct |
253 ms |
59232 KB |
Output is correct |
72 |
Correct |
255 ms |
59072 KB |
Output is correct |
73 |
Correct |
239 ms |
55668 KB |
Output is correct |
74 |
Correct |
231 ms |
55772 KB |
Output is correct |
75 |
Correct |
225 ms |
54492 KB |
Output is correct |
76 |
Correct |
230 ms |
54600 KB |
Output is correct |
77 |
Correct |
226 ms |
53344 KB |
Output is correct |
78 |
Correct |
226 ms |
52196 KB |
Output is correct |
79 |
Correct |
231 ms |
57372 KB |
Output is correct |