#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[16001],dp[16001];
struct b{
int last,to;
}v[32001];
int first[16001];
void push(int i,int x,int y){
v[i].last=first[x];
first[x]=i;
v[i].to=y;
return;
}
int book[16001],ans=0;
int dg(int i,int f){
dp[i]=a[i];
for(int u=first[i];u;u=v[u].last){
if(f!=v[u].to){
dg(v[u].to , i);
if(dp[v[u].to]>0){
dp[i]+=dp[v[u].to];
}
}
}
ans=max(ans,dp[i]);
return 0;
}
signed main(){
cin >> n;
for(int i=1;i<=n;i++)cin >> a[i];
for(int i=1;i<n;i++){
int x,y;
cin >> x >> y;
push(i,x,y);
push(i+n,y,x);
}
dg(1,0);
cout << ans;
return 0;
}