#include<iostream>
#include<cstring>
using namespace std;
const int N = 6010;
int h[N], e[N], ne[N], idx;
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int happy[N], n;
int ind[N];
typedef pair<int, int> PII;
PII dfs(int u){
int a = 0, b = happy[u];
for(int i = h[u]; i != -1; i = ne[i]){
int v = e[i];
auto t = dfs(v);
a += max(t.first, t.second);
b += t.first;
}
return {a, b};
}
int main(){
cin >> n;
for(int i = 1; i <= n; i ++ ) cin >> happy[i];
memset(h, -1, sizeof h);
for(int i = 1; i < n; i ++ ){
int a, b;
cin >> a >> b;
add(b, a);
ind[a] ++ ;
}
int root = 1;
while(ind[root]) root ++ ;
auto t = dfs(root);
cout << max(t.first, t.second) << endl;
}