# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
7581 | jihoon | Friend (IOI14_friend) | C++98 | 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<cstdio>
#include<algorithm>
using namespace std;
int n;
int confi[100001];
int host[100001];
int protocol[100001];
int dp[100001][2];
int main(){
int i;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&confi[i]);
dp[i][0]=confi[i];
}
for(i=1;i<n;i++){
scanf("%d",&host[i]);
scanf("%d",&protocol[i]);
}
for(i=n-1;i>=1;i--){
if(protocol[i]==0){
dp[host[i]][0]+=dp[i][1];
dp[host[i]][1]=max(dp[host[i]][1]+dp[i][1],dp[host[i]][1]+dp[i][0]);
}else if(protocol[i]==1){
dp[host[i]][0]=max(dp[host[i]][0]+dp[i][0],max(dp[host[i]][0]+dp[i][1],dp[host[i]][1]+dp[i][0]));
dp[host[i]][1]+=dp[i][1];
}else{
dp[host[i]][0]=max(dp[host[i]][0]+dp[i][1],dp[host[i]][1]+dp[i][0]);
dp[host[i]][1]+=dp[i][1];
}
}
printf("%d",max(dp[0][0],dp[0][1]));
}