树形dp
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5, M = N * 2;
typedef long long LL;
int n, h[N], e[M], ne[M], idx;
LL ans, g[N];
void add(int a, int b){
e[idx] = b; ne[idx] = h[a]; h[a] = idx ++;
}
LL dfs(int u, int fa){
LL sum = g[u];
for(int i = h[u]; i != -1; i = ne[i]){
int j = e[i];
if(j != fa){
LL d = dfs(j, u);
if(d > 0) sum += d;
}
}
ans = max(ans, sum);
return sum;
}
int main(){
ans = -2e9;
memset(h, -1, sizeof h);
cin >> n;
for(int i = 1; i <= n; i ++ ){
scanf("%lld", &g[i]);
}
for(int i = 1; i < n; i ++ ) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b); add(b, a);
}
dfs(1, -1); // u, fa
cout << ans << endl;
return 0;
}