# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
471025 | iulia13 | 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 <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int dp[N][2];
int c[N], h[N], p[N];
int findSample(int n, int confidence[], int host[], int protocol[])
{
int i;
for (i = 0; i < n; i++)
dp[i][1] = confidence[i];
for (i = n - 1; i; i--)
{
int j = host[i];
if (protocol[i] == 0)
{
dp[j][0] += max(dp[i][0], dp[i][1]);
dp[j][1] += dp[i][0];
continue;
}
int t = dp[i][0];
if (protocol[i] == 1)
t = max(t, dp[i][1]);
dp[j][1] += t;
dp[j][1] = max(dp[j][1], dp[j][0] + dp[i][1]);
dp[j][0] += dp[i][0];
}
return max(dp[0][0], dp[0][1]);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> c[i];
for (int i = 1; i < n; i++)
cin >> h[i] >> p[i];
cout << findSample(n, c, h, p);
return 0;
}