# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
31077 | WhipppedCream | 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>
#define ii pair<int, int>
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vii vector< pair<int, int> >
typedef long long ll;
using namespace std;
int Host[100005];
int Prot[100005];
int conf[100005];
int c[100005];
int nc[100005];
int main()
{
int n; scanf("%d", &n);
for(int i = 0; i< n; i++) scanf("%d", conf+i), c[i] = conf[i];
for(int i = 1; i< n; i++) scanf("%d %d", Host+i, Prot+i);
for(int i = n-1; i>= 1; i--)
{
int x = Host[i];
if(Prot[i] == 1)
{
c[x] = max(max(c[x]+nc[i], c[i]+nc[x]), c[i]+c[x]);
nc[x] = nc[x] + nc[i];
}
else if(Prot[i] == 2)
{
c[x] = max(c[x]+nc[i], c[i]+nc[x]);
nc[x] = nc[x]+nc[i];
}
else
{
c[x] = c[x]+nc[i];
nc[x] = max(c[i]+nc[x], nc[i]+nc[x]);
}
}
printf("%d\n", max(c[0], nc[0]));
return 0;
}