#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000;
int head[maxn],to[maxn],nt[maxn],cnt = 0;
int happy[maxn],f[maxn][2],l[maxn];
int n,root=1;
void add(int x,int y){
cnt++;
to[cnt] = y;
nt[cnt] = head[x];
head[x] = cnt;
}
void dfs(int u,int fa){
f[u][0] = 0;
f[u][1] = happy[u];
for(int i = head[u];i;i=nt[i]){
int v = to[i];
if(v == fa) continue;
dfs(v,u);
f[u][0] += max(f[v][0],f[v][1]);
f[u][1] += f[v][0];
}
}
int main(){
freopen("P135_2.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&happy[i]);
int x,y,m1 = 0,m2 = -1;
for(int i=1;i<=n-1;i++){
scanf("%d %d",&x,&y);
if(x != y) l[x]++;
add(x,y);
add(y,x);
}
while(l[root] != 0) root++;
dfs(root,0);
printf("%d",max(f[root][0],f[root][1]));
return 0;
}