#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 300010;
int n, m;
int p[2 * MAX];
ll c[2 * MAX];
vector<pair<int, ll>> g[MAX];
struct state
{
ll cost, dist, free_increase;
};
state dp(int u)
{
if (u > n)
return {0, 0, 0};
ll res = 0;
vector<state> T;
ll min_val = 0;
ll free_increase = LLONG_MAX;
for (auto [v, l] : g[u])
{
auto [t, d, free] = dp(v);
res += t;
T.push_back({l, d, free});
free_increase = min(free_increase, free);
min_val = max(min_val, d);
}
sort(T.begin(), T.end(), [] (const state &A, const state &B) {return A.cost + A.dist < B.cost + B.dist;});
for (int i = 0; i < T.size() / 2; i++)
{
ll inc = min(T[i].free_increase, T[T.size()/2].cost + T[T.size()/2].dist - T[i].cost - T[i].dist);
T[i].dist += inc;
T[i].free_increase -= inc;
free_increase = min(free_increase, T[i].free_increase);
}
ll best = T[(T.size()-1)/2].cost + T[(T.size()-1)/2].dist, free = T[T.size()/2].cost + T[T.size()/2].dist - T[(T.size()-1)/2].cost - T[(T.size()-1)/2].dist;
if (min_val >= T[T.size()/2].cost + T[T.size()/2].dist)
best = min_val, free = 0;
else if (min_val >= best)
best = min_val, free = T[T.size()/2].cost + T[T.size()/2].dist - min_val;
for (state x : T)
res += abs(best - x.cost - x.dist);
return {res, best, free_increase + free};
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
p[1] = -1, c[1] = 0;
for (int i = 2; i <= n + m; i++)
{
cin >> p[i] >> c[i];
g[p[i]].push_back({i, c[i]});
}
cout << dp(1).cost << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'state dp(int)':
fireworks.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 0; i < T.size() / 2; i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7252 KB |
Output is correct |
12 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7252 KB |
Output is correct |
12 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |