#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];
ll f(vector<ll> &t, ll c)
{
ll ans = 0;
for (ll x : t)
ans += abs(c - x);
return ans;
}
pair<ll, ll> dp(int u)
{
if (u > n)
return {0, 0};
ll res = 0;
vector<ll> times;
for (auto [v, l] : g[u])
{
auto [t, d] = dp(v);
res += t;
times.push_back(l + d);
}
vector<ll> t = times;
sort(t.begin(), t.end());
ll lo = 0, hi = t.size() - 1, m1, m2, f1, f2;
while (hi - lo + 1 > 3)
{
m1 = lo + (hi - lo) / 3;
m2 = hi - (hi - lo) / 3;
f1 = f(times, t[m1]);
f2 = f(times, t[m2]);
if (f1 < f2)
hi = m2 - 1;
else if (f1 > f2)
lo = m1 + 1;
else if (f1 == f2)
lo++, hi--;
}
ll best = t[lo], cur = LLONG_MAX;
for (ll c = lo; c <= hi; c++)
{
ll maybe = f(times, t[c]);
if (maybe < cur)
best = t[c], cur = maybe;
}
for (int i = 0; i < g[u].size(); i++)
if (times[i] - best > g[u][i].second)
best = max(best, times[i] - g[u][i].second);
for (int i = 0; i < g[u].size(); i++)
res += abs(times[i] - best);
return {res, best};
}
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).first << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'std::pair<long long int, long long int> dp(int)':
fireworks.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < g[u].size(); i++)
| ~~^~~~~~~~~~~~~
fireworks.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < g[u].size(); i++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 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 |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 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 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 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 |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 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 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 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 |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 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 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |