# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1083539 | jay22 | Friend (IOI14_friend) | C++14 | 0 ms | 0 KiB |
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 <iostream>
#include <algorithm>
const int LEN = 100'000;
int N, w[LEN], dp[LEN][2], host[LEN], protocol[LEN];
int main() {
std::cin >> N;
for (int i = 0; i < N; ++i) std::cin >> w[i];
for (int i = 1; i < N; ++i) std::cin >> host[i] >> protocol[i];
for (int i = N - 1; i; --i) {
if (protocol[i] == 0) {
dp[host[i]][1] += dp[i][0];
dp[host[i]][0] += std::max(dp[i][0], dp[i][1] + w[i]);
}
if (protocol[i] == 1) {
dp[host[i]][0] += dp[i][0];
dp[host[i]][1] += std::max(dp[i][0], dp[i][1] + w[i]);
}
if (protocol[i] == 2) {
dp[host[i]][0] += dp[i][0];
dp[host[i]][1] += std::max(dp[i][0], dp[i][1]);
w[host[i]] = std::max(w[host[i]], w[i]);
}
}
std::cout << std::max(dp[0][0], dp[0][1] + w[0]);
}