// Grzegorz Ryn
// V LO Kraków
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define st first
#define nd second
const ll INF = 1'000'000'000;
int N, M;
vector<vector<int>> g;
vector<ll> cost;
vector<int> slope_ind;
vector<priority_queue<ll>> slope;
vector<ll> initial_slope, final_slope, first_value;
void dfs(int v) {
if(v > N) {
slope[v].push(cost[v]);
slope[v].push(cost[v]);
initial_slope[v] = -1;
final_slope[v] = 1;
first_value[v] = cost[v];
return;
}
for(int u:g[v]) {
dfs(u);
int V = slope_ind[v];
int U = slope_ind[u];
if(slope[V].size() < slope[U].size()) {
swap(V, U);
swap(slope_ind[v], slope_ind[u]);
}
while(slope[U].size() > 0) {
slope[V].push(slope[U].top());
slope[U].pop();
}
first_value[v] += first_value[u];
initial_slope[v] += initial_slope[u];
final_slope[v] += final_slope[u];
}
ll V = slope_ind[v], l, r;
while(final_slope[v] >= 0) {
if(final_slope[v] == 1) {
r = slope[V].top();
}
else if(final_slope[v] == 0) {
l = slope[V].top();
}
slope[V].pop();
final_slope[v]--;
}
first_value[v] += cost[v];
slope[V].push(l + cost[v]);
slope[V].push(r + cost[v]);
final_slope[v] = 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
g.resize(N+M+1);
cost.resize(N+M+1, 0);
slope.resize(N+M+1);
slope_ind.resize(N+M+1);
iota(slope_ind.begin(), slope_ind.end(), 0);
initial_slope.resize(N+M+1, 0);
final_slope.resize(N+M+1, 0);
first_value.resize(N+M+1, 0);
for(int i=2; i<=N+M; i++) {
int p;
cin >> p >> cost[i];
g[p].pb(i);
}
dfs(1);
int it = slope_ind[1];
ll result = (ll)INF*INF, res = first_value[1], prv = 0;
vector<ll> pos;
while(slope[it].size() > 0) {
pos.pb(slope[it].top());
slope[it].pop();
}
reverse(pos.begin(), pos.end());
for(ll p : pos) {
res += initial_slope[1] * (p - prv);
initial_slope[1]++;
prv = p;
result = min(result, res);
if(initial_slope[1] >= 0) {
break;
}
}
cout << result << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:68:18: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
68 | slope[V].push(r + cost[v]);
fireworks.cpp:67:18: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | slope[V].push(l + cost[v]);
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
580 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
716 KB |
Output is correct |
35 |
Correct |
2 ms |
844 KB |
Output is correct |
36 |
Correct |
4 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
972 KB |
Output is correct |
38 |
Correct |
3 ms |
1100 KB |
Output is correct |
39 |
Correct |
4 ms |
1100 KB |
Output is correct |
40 |
Correct |
3 ms |
1688 KB |
Output is correct |
41 |
Correct |
4 ms |
1604 KB |
Output is correct |
42 |
Correct |
2 ms |
972 KB |
Output is correct |
43 |
Correct |
3 ms |
1356 KB |
Output is correct |
44 |
Correct |
3 ms |
1228 KB |
Output is correct |
45 |
Correct |
3 ms |
1228 KB |
Output is correct |
46 |
Correct |
3 ms |
1244 KB |
Output is correct |
47 |
Correct |
3 ms |
1228 KB |
Output is correct |
48 |
Correct |
3 ms |
1100 KB |
Output is correct |
49 |
Correct |
3 ms |
1228 KB |
Output is correct |
50 |
Correct |
2 ms |
1024 KB |
Output is correct |
51 |
Correct |
2 ms |
948 KB |
Output is correct |
52 |
Correct |
3 ms |
1100 KB |
Output is correct |
53 |
Correct |
3 ms |
1100 KB |
Output is correct |
54 |
Correct |
3 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
580 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
716 KB |
Output is correct |
35 |
Correct |
2 ms |
844 KB |
Output is correct |
36 |
Correct |
4 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
972 KB |
Output is correct |
38 |
Correct |
3 ms |
1100 KB |
Output is correct |
39 |
Correct |
4 ms |
1100 KB |
Output is correct |
40 |
Correct |
3 ms |
1688 KB |
Output is correct |
41 |
Correct |
4 ms |
1604 KB |
Output is correct |
42 |
Correct |
2 ms |
972 KB |
Output is correct |
43 |
Correct |
3 ms |
1356 KB |
Output is correct |
44 |
Correct |
3 ms |
1228 KB |
Output is correct |
45 |
Correct |
3 ms |
1228 KB |
Output is correct |
46 |
Correct |
3 ms |
1244 KB |
Output is correct |
47 |
Correct |
3 ms |
1228 KB |
Output is correct |
48 |
Correct |
3 ms |
1100 KB |
Output is correct |
49 |
Correct |
3 ms |
1228 KB |
Output is correct |
50 |
Correct |
2 ms |
1024 KB |
Output is correct |
51 |
Correct |
2 ms |
948 KB |
Output is correct |
52 |
Correct |
3 ms |
1100 KB |
Output is correct |
53 |
Correct |
3 ms |
1100 KB |
Output is correct |
54 |
Correct |
3 ms |
1100 KB |
Output is correct |
55 |
Correct |
8 ms |
2380 KB |
Output is correct |
56 |
Correct |
45 ms |
8164 KB |
Output is correct |
57 |
Correct |
63 ms |
13440 KB |
Output is correct |
58 |
Correct |
84 ms |
17408 KB |
Output is correct |
59 |
Correct |
110 ms |
22892 KB |
Output is correct |
60 |
Correct |
149 ms |
28220 KB |
Output is correct |
61 |
Correct |
169 ms |
32756 KB |
Output is correct |
62 |
Correct |
196 ms |
35980 KB |
Output is correct |
63 |
Correct |
234 ms |
42740 KB |
Output is correct |
64 |
Correct |
267 ms |
44852 KB |
Output is correct |
65 |
Correct |
122 ms |
83852 KB |
Output is correct |
66 |
Correct |
116 ms |
83784 KB |
Output is correct |
67 |
Correct |
129 ms |
41708 KB |
Output is correct |
68 |
Correct |
167 ms |
66760 KB |
Output is correct |
69 |
Correct |
211 ms |
63272 KB |
Output is correct |
70 |
Correct |
214 ms |
63284 KB |
Output is correct |
71 |
Correct |
213 ms |
60060 KB |
Output is correct |
72 |
Correct |
218 ms |
60076 KB |
Output is correct |
73 |
Correct |
214 ms |
56800 KB |
Output is correct |
74 |
Correct |
183 ms |
56832 KB |
Output is correct |
75 |
Correct |
183 ms |
55380 KB |
Output is correct |
76 |
Correct |
223 ms |
55584 KB |
Output is correct |
77 |
Correct |
187 ms |
54032 KB |
Output is correct |
78 |
Correct |
186 ms |
52876 KB |
Output is correct |
79 |
Correct |
193 ms |
52464 KB |
Output is correct |