This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
int main() {
using namespace std;
int N, M, K;
cin >> N >> M >> K;
vector<int> P(N + 1);
for (int i = 2; i <= N; i++)
cin >> P[i];
vector<int> D(N + 1, -1);
vector<long long> W(N + 1);
for (int i = 0; i < M; i++) {
int u, d;
long long w;
cin >> u >> d >> w;
tie(D[u], W[u]) = make_pair(d, w);
}
vector<map<int, long long>> A(N + 1);
for (int i = N; i > 1; i--) {
if (D[i] != -1) {
auto it = A[i].emplace(D[i], 0).first;
it->second += W[i];
for (it = next(it);
it != A[i].end() && W[i] >= it->second;
W[i] -= it->second, it = A[i].erase(it)
);
if (it != A[i].end())
it->second -= W[i];
}
if (A[i].size() > A[P[i]].size())
swap(A[i], A[P[i]]);
for (auto [k, v] : A[i])
A[P[i]][k] += v;
}
long long ans = 0;
for (auto [k, v] : A[1])
ans += v;
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |