#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string> using namespace std; #define maxn 6005
struct node{
int to,net;
}que[maxn<<];
int head[maxn];
int n;
int dp[maxn][],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
bool visited[maxn];
int tot=; void addedge(int u,int v){
que[tot].to=v;
que[tot].net=head[u];
head[u]=tot++; que[tot].to=u;
que[tot].net=head[v];
head[v]=tot++; } void tree_dp(int node)
{
int i;
visited[node] = ;
for(i=head[node]; i!=-; i=que[i].net)
{
int v=que[i].to;
if(!visited[v]&&father[v] == node)//i为下属
{
tree_dp(v);//递归调用孩子结点,从叶子结点开始dp
//关键
) dp[node][] += dp[v][];//上司来,下属不来
dp[node][] +=max(dp[v][],dp[v][]);//上司不来,下属来、不来
}
}
} int main()
{
int i;
int f,c,root;
while(scanf("%d",&n)!=EOF)
{
tot=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
memset(father,,sizeof(father));
memset(visited,,sizeof(visited));
for(i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
root = ;//记录父结点
bool beg = ;
while (scanf("%d %d",&c,&f),c||f)
{
addedge(c,f);
father[c] = f;
if( root == c || beg )
{
root = f;
}
}
while(father[root])//查找父结点
root=father[root];
tree_dp(root);
int imax=max(dp[root][],dp[root][]);
printf("%d\n",imax);
}
return ; }
04-14 06:33