Pagini recente » Cod sursa (job #1312411) | Cod sursa (job #496830) | Cod sursa (job #1712046) | Cod sursa (job #2315666) | Cod sursa (job #13590)
Cod sursa(job #13590)
#include<stdio.h>
#include<stdlib.h>
#define maxn 16010
int n, v[maxn], max=-1000*16000-10, uz[maxn], maxi, *leg[maxn], sum=0, s[maxn];
//struct nod{ int x, y;} l[maxn];
int dfs(int x)
{
uz[x]=1;
s[x]=v[x];
for(int i=1; i<=leg[x][0]; ++i) if( !uz[ leg[x][i]])
s[x]+= dfs(leg[x][i]);
uz[x]=0;
if( s[x]<0) return 0;
return s[x];
}
int main()
{
freopen("asmax.in","r",stdin);
scanf("%d",&n);
for(int i=1; i<=n; ++i) {
leg[i]= (int*)malloc( sizeof(int)*2);
leg[i][0]=0;
}
for(int i=1; i<=n; ++i) scanf("%d",&v[i]);
int x, y;
for(int i=1; i<=n-1; ++i) {
scanf("%d %d",&x,&y);
leg[x][0]++;
leg[x]=(int*)realloc(leg[x], sizeof(int)*(leg[x][0]+5));
leg[x][ leg[x][0]]= y;
leg[y][0]++;
leg[y]=(int*)realloc(leg[y], sizeof(int)*(leg[y][0]+5));
leg[y][ leg[y][0]]= x;
}
// for(int i=5; i<=n; ++i) {
dfs(1);
for(int i=1; i<=n; ++i) if( s[i]>max) max=s[i];
// }
freopen("asmax.out","w",stdout);
printf("%d\n",max);
return 0;
}