include[HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
int maxn,dp[6001][2],a[6001],son[6001],brother[6001],father[6001];
void f(int x){
dp[x][1]=a[x];
for(int i=son[x];i!=-1;i=brother[i]){
int j=son[i];
f(i);
dp[x][1]+=dp[i][0];
dp[x][0]+=max(dp[i][1],dp[i][0]);
}
}
int main(){
memset(son,-1,sizeof(son));
memset(brother,-1,sizeof(brother));
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i[HTML_REMOVED]>x>>y;
father[x]=1;
if(son[y]==-1)
son[y]=x;
else{
brother[x]=son[y];
son[y]=x;
}
}
int root=1;
while(father[root])root++;
int x=root;
f(root);
int ans;
ans=max(dp[root][0],dp[root][1]);
cout<<ans<<endl;
return 0;
}