https://www.acwing.com/problem/content/287/
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+9;
vector<int> son[N];
int n;
int happy[N];
bool ju[N];
int f[N][2];
void dfs(int u){
f[u][1]=happy[u];
for(auto it:son[u]){
dfs(it);
f[u][0]+=max(f[it][0],f[it][1]);
f[u][1]+=f[it][0];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>happy[i];
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
son[b].push_back(a);
ju[a]=true;
}
int root;
for(int i=1;i<=n;i++) if(!ju[i])root=i;
dfs(root);
cout<<max(f[root][0],f[root][1]);
return 0;
}