#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, lo, hi;
};
pair<ll,ll> dp(int u, ll dist=0, ll len=-1)
{
if (u > n)
return {0, 0};
ll res = 0;
vector<ll> T;
ll min_val = 0;
for (auto [v, l] : g[u])
{
auto [t, d] = dp(v, dist + l, len);
res += t;
T.push_back(l + d);
min_val = max(min_val, d);
}
sort(T.begin(), T.end());
ll best = T[(T.size() - 1) / 2];
if (len != -1)
{
if (dist + best <= len && len <= dist + T[T.size()/2])
best = len - dist;
else if (abs(dist + best - len) > abs(dist + T[T.size()/2] - len))
best = T[T.size()/2];
}
if (min_val >= best)
best = min_val;
for (ll x : T)
res += abs(best - x);
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, 0, dp(1).second).first << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7348 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7380 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 |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7348 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
6 ms |
7380 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 |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7348 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
6 ms |
7380 KB |
Output is correct |
12 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |