#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<map>
using namespace std;
const int MAXN = 200010;
const int MOD = 1e9+7;
#define INF 0x3f3f3f3f
#define LL long long
int cnt;
int mx = -INF, mn = INF;
int TT, T, n, m;
int numw;
int h[6010];
struct stu
{
int v;
int next;
}w[601000];
void add(int u, int v)
{
w[++numw].v = v;
w[numw].next = h[u];
h[u] = numw;
}
int dp[6010][2];
int a[6001];
void dfs(int k)
{
int i, j;
int s1 = 0, s2 = 0;
if(!h[k])
{
return ;
}
for(i = h[k]; i ; i = w[i].next)
{
int to = w[i].v;
dfs(to);
dp[k][1] += dp[to][0];
dp[k][0] += max(dp[to][1], dp[to][0]);
}
}
int main()
{
int ans, fla;
int i, j, k, kk, l, r;
int fa[6010];
cnt = 1;
while(scanf("%d",&n) != EOF)
{
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
memset(h, 0, sizeof(h));
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; i++)
{
fa[i] = i;
}
numw = 0;
while(scanf("%d %d", &l, &r) != EOF)
{
if(l == 0 && r == 0)
break;
fa[l] = r;
add(r, l);
}
int rt;
for(i = 1; i <= n; i++)
{
dp[i][1] = a[i];
if(fa[i] == i)
rt = i;
}
dfs(rt);
cout << max(dp[rt][1], dp[rt][0]) << endl;
}
return 0;
}