#include <iostream>
#include <cstdio>
#include <cstring>
struct Tree{
int father;
int child;
int brother;
int NOT;
int s;
int Max(){
return s > NOT ? s : NOT;
}
void Init(){
father=child=brother=NOT=0;
}
}tree[60001];
void dfs(int v)
{
int child;
child=tree[v].child;
while(child){
dfs(child);
tree[v].s+=tree[child].NOT;
tree[v].NOT+=tree[child].Max();
child=tree[child].brother;
}
}
int main()
{
int n;
while(~scanf("%d",&n)){
for(int i=1; i<=n; i++){
scanf("%d",&tree[i].s);
tree[i].Init();
}
int f,c;
while(scanf("%d%d",&c,&f)&&f+c){
tree[c].father=f;
tree[c].brother=tree[f].child;
tree[f].child=c;
}
for(int i=1;i<=n;i++){
if(!tree[i].father){
dfs(i);
printf("%d\n",tree[i].Max());
}
}
}
}