九十分,wa了一个点,不知道错哪了
#include<bits/stdc++.h>
using namespace std;
int n;
int a[16005],dp[16005];
struct b{
int last,to;
}v[32005];
int first[16005];
void push(int i,int x,int y){
v[i].last=first[x];
first[x]=i;
v[i].to=y;
return;
}
int ans=0;
int dq(int i,int f){
dp[i]=a[i];
for(int k=first[i];k;k=v[k].last){
if(f!=v[k].to){
dq(v[k].to,i);
if(dp[v[k].to]>0){
dp[i]+=dp[v[k].to];
}
}
}
ans=max(ans,dp[i]);
return 0;
}
int main(){
cin >> n;
for(int i=1;i<=n;i++)cin >> a[i];
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
push(i,x,y);
push(i+n,y,x);
}
dq(1,0);
cout<<ans;
return 0;
}
//-10086这是样例