/*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]), vec.push_back(R[j]);
sort(vec.begin(), vec.end());
int l = vec[children[i].size() - 1], r = vec[children[i].size()];
for (int j : children[i]) {
if (l < L[j]) ans += L[j] - l;
else if (l > R[j]) ans += l - R[j];
}
L[i] = l + C[i], R[i] = r + C[i];
// cout << i << ": " << L[i] << ' ' << R[i] << 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 |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
9 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 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 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
8 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 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 |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
9 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 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 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
8 ms |
14464 KB |
Output is correct |
11 |
Correct |
8 ms |
14464 KB |
Output is correct |
12 |
Correct |
9 ms |
14464 KB |
Output is correct |
13 |
Incorrect |
9 ms |
14464 KB |
Output isn't correct |
14 |
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 |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14464 KB |
Output is correct |
5 |
Correct |
9 ms |
14464 KB |
Output is correct |
6 |
Correct |
8 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 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
8 ms |
14464 KB |
Output is correct |
11 |
Correct |
8 ms |
14464 KB |
Output is correct |
12 |
Correct |
9 ms |
14464 KB |
Output is correct |
13 |
Incorrect |
9 ms |
14464 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |