Pagini recente » racovita_11_12_corect | Cod sursa (job #927572) | Cod sursa (job #248132) | Cod sursa (job #2191200) | Cod sursa (job #1481904)
#include<stdio.h>
#define N 16000
int edges[N*2+2],next[N*2+2],last[N*2+2],first[N*2+2],viz[N+1],v[N+1];
void dfs(int nod){
int p=first[nod];
viz[nod]=1;
while(p!=0){
if(viz[edges[p]]==0){
dfs(edges[p]);
if(v[edges[p]]>0)
v[nod]+=v[edges[p]];
}
p=next[p];
}
}
int main(){
freopen("asmax.in","r",stdin);
freopen("asmax.out","w",stdout);
int n;
scanf("%d",&n);
int i,cont=1;
for(i=1;i<=n;i++)
scanf("%d",&v[i]);
for(i=0;i<n-1;i++){
int x,y;
scanf("%d%d",&x,&y);
if(first[x]==0){
first[x]=cont;
last[x]=cont;
}
else{
next[last[x]]=cont;
last[x]=cont;
}
edges[cont++]=y;
if(first[y]==0){
first[y]=cont;
last[y]=cont;
}
else{
next[last[y]]=cont;
last[y]=cont;
}
edges[cont++]=x;
}
dfs(1);
int max=-16000000;
for(i=1;i<=n;i++)
if(v[i]>max)
max=v[i];
printf("%d",max);
return 0;
}