/*input
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cassert>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORA(i, a) for (auto &i : a)
#define FORB(i, a, b) for (int i = a; i >= b; --i)
const int MAXN = 300007;
int P[MAXN], C[MAXN], N, M, D[MAXN], L[MAXN], R[MAXN];
vector<int> events[MAXN];
vector<int> children[MAXN];
long long ans = 0;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
FOR(i, 2, N + M) {
cin >> P[i] >> C[i];
children[P[i]].push_back(i);
}
FOR(i, 1, N) assert(!children[i].empty());
FOR(i, N + 1, N + M) assert(children[i].empty());
FOR(i, N + 1, N + M) L[i] = C[i], R[i] = C[i];
FORB(i, N, 1) {
vector<int> vec;
for (int j : children[i]) vec.push_back(L[j] * 2), vec.push_back(R[j] * 2 + 1);
sort(vec.begin(), vec.end());
int l = -1, r = -1;
long long cur = 0, best;
int last = 0, a = (int) children[i].size();
for (int j : children[i]) cur += L[j];
best = cur;
// cout << i << "= ";
// for (int x : vec) cout << x << ' '; cout << endl;
for (auto it = vec.begin(); it != vec.end();) {
int x = *it;
cur -= (x / 2 - last) * a, last = x / 2;
if (cur == best) r = x;
for (; it != vec.end() && *it == x; ++it) --a;
if (cur < best) best = cur, l = x, r = x;
}
l /= 2, r /= 2;
for (int j : children[i]) {
if (l < L[j]) ans += L[j] - l;
else if (l > R[j]) ans += l - R[j];
// cout << "+ " << j << ": " << ans << endl;
}
L[i] = l + C[i], R[i] = r + C[i];
// cout << i << ": " << l << ' ' << r << " + " << L[i] << ' ' << R[i] << " - " << ans << endl;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
9 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Incorrect |
9 ms |
14464 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Incorrect |
9 ms |
14464 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
9 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Incorrect |
9 ms |
14464 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
8 ms |
14464 KB |
Output is correct |
3 |
Correct |
9 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
8 ms |
14464 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
8 ms |
14464 KB |
Output is correct |
8 |
Correct |
8 ms |
14464 KB |
Output is correct |
9 |
Incorrect |
9 ms |
14464 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |