// 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 int INF = 1'000'000'000;
int N, M;
vector<vector<int>> g;
vector<ll> cost;
vector<int> slope_ind;
vector<vector<int>> slope;
vector<int> initial_slope, final_slope;
vector<ll> first_value;
void dfs(int v) {
if(v > N) {
slope[v].pb(cost[v]);
slope[v].pb(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]);
}
for(int p : slope[U]) {
slope[V].pb(p);
}
sort(slope[V].begin(), slope[V].end());
first_value[v] += first_value[u];
initial_slope[v] += initial_slope[u];
final_slope[v] += final_slope[u];
}
int V = slope_ind[v], l, r;
while(final_slope[v] >= 0) {
if(final_slope[v] == 1) {
r = slope[V].back();
}
else if(final_slope[v] == 0) {
l = slope[V].back();
}
slope[V].pop_back();
final_slope[v]--;
}
first_value[v] += cost[v];
slope[V].pb(l + cost[v]);
slope[V].pb(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);
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(2);
int it = slope_ind[2];
ll result = (ll)INF*INF, res = first_value[2], prv = 0;
for(ll pos : slope[it]) {
res += initial_slope[2] * (pos - prv);
initial_slope[2]++;
prv = pos;
result = min(result, res);
}
cout << result << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:69:16: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | slope[V].pb(r + cost[v]);
fireworks.cpp:68:16: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
68 | slope[V].pb(l + cost[v]);
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |