Pagini recente » Cod sursa (job #1153424) | Cod sursa (job #29184) | Cod sursa (job #487537) | Cod sursa (job #2715085) | Cod sursa (job #554119)
Cod sursa(job #554119)
#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],sum[dim],tata[dim];
long long max=-99999999;
void bfs()
{
i=1; j=1; viz[1]=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; tata[j]=x;}
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(); sum[nod[n]]=cost[nod[n]];
for(i=n; i>=1; i--)
{x=nod[i];
if(cost[x]+sum[x-1]>sum[x]) {sum[x]=cost[x]+sum[x-1]; if(sum[tata[x]]<sum[x]) sum[tata[x]]=sum[x];}
else sum[x]=cost[x];
if(sum[x]>max) max=sum[x];
}
fprintf(g,"%lld\n",max);
fclose(f);
fclose(g);
return 0;
}