树形DP第二道
#include <bits/stdc++.h>
#define read read()
using namespace std;
const int N = 20000;
int n,size,ans;
int head[N],a[N];
//int f[N][2];
int f[N];
int read
{
int x = 0,f = 1; char ch = getchar();
while( ch < 48 || ch > 57 ) {if(ch == '-') f = -1; ch = getchar();}
while(ch >= 48 && ch <= 57) {x = 10 * x + ch - 48; ch = getchar();}
return x * f;
}
struct edge{
int v,nxt;
}e[N<<1];
void add(int u,int v)
{
e[++size].v = v;
e[size].nxt = head[u];
head[u] = size;
}
void dp(int u,int fa)
{
//f[u][0] = 0; f[u][1] = a[u];
f[u] = a[u];
for(int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if(v == fa) continue;
dp(v,u);
//f[u][0] = 0;
//f[u][1] += max
f[u] += max(0,f[v]);
}
ans = max(ans, f[u]);
}
int main()
{
// freopen("sontree.in","r",stdin);
n = read; int u,v;
for(int i = 1; i <= n; i++) a[i] = read;
for(int i = 1; i < n; i++)
{
u = read; v = read;
add(u,v);
add(v,u);
}
dp(1,0);
printf("%d",ans);
return 0;
}