#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,happy[N];
int f[N][3];
int a[N][N];
int b[N];
bool fa[1005];
void dfs(int u)
{
f[u][1]=happy[u];
for(int i=0;i<b[u];i++)
{
int son=a[u][i];
dfs(son);
f[u][0]+=max(f[son][0],f[son][1]);
f[u][1]+=f[son][0];
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>happy[i];
for(int i=0;i<n-1;i++)
{
int x,y;
cin>>x>>y;
a[y][b[y]++]=x;
fa[x]=true;
}
int root=1;
while(fa[root])root++;
dfs(root);
cout<<max(f[root][1],f[root][0]);
return 0;
}