Pagini recente » Cod sursa (job #906405) | Cod sursa (job #2723890) | Cod sursa (job #2975200) | Cod sursa (job #2104634) | Cod sursa (job #928207)
Cod sursa(job #928207)
#include<stdio.h>
#define dim 16005
using namespace std;
int i,n,p,x,j,y,rad,cost[dim],mat[dim][dim],viz[dim],nod[dim],s[dim];
long long max=-99999999;
void bfs()
{
i=1; j=1; viz[1]=1; s[1]=cost[1]; nod[i]=1;
while(i<=j)
{x = nod[i];
for(p=1; p<=mat[x][0]; p++)
if(!viz[mat[x][p]])
{
y = mat[x][p];
viz[y] = 1;
j++; nod[j] = y;
if(s[x] + cost[y] >= 0) {s[x] = s[x] + cost[y]; s[y] = s[x];}
else s[y] = cost[y];
if(s[y] > max) max = s[y];
}
i++;
}
}
int main()
{
FILE *f=fopen("asmax.in","r"), *g=fopen("asmax.out","w");
fscanf(f,"%d",&n);
for(i=1; i<=n; i++)
fscanf(f,"%d",&cost[i]);
for(i=1; i<n; i++)
{fscanf(f,"%d%d",&x,&y);
mat[x][0]++; mat[x][mat[x][0]]=y;
mat[y][0]++; mat[y][mat[y][0]]=x;
}
bfs();
fprintf(g,"%lld\n",max);
fclose(f);
fclose(g);
return 0;
}