#include <iostream>
#include <cstdio>
const int N = 6e3 + 7;
int a[N];
int f[2][N];
bool vis[N];
int head[N], nex[N], to[N], cnt;
bool is_son[N];
void add(int u, int v) {
nex[++cnt] = head[u];
head[u] = cnt;
to[cnt] = v;
}
void dfs(int k) {
vis[k] = true;
for(int i = head[k]; i; i = nex[i]) {
if(vis[to[i]]) continue;
dfs(to[i]);
f[1][k] += f[0][to[i]];
f[0][k] += std::max(f[0][to[i]], f[1][to[i]]);
}
return;
}
int main() {
int n;
std::cin >> n;
for(int i = 1; i <= n; i++)
std::cin >> f[1][i];
for(int i = 1; i < n; i++) {
int x, y;
std::cin >> x >> y;
add(x, y);
is_son[y] = true;
}
for(int i = 1; i <= n; i++)
if(!is_son[i]) {
dfs(i);
std::cout << std::max(f[0][i], f[1][i]);
return 0;
}
return 0;
}
如果犯蠢请谅解