Pagini recente » Cod sursa (job #1034991) | Cod sursa (job #1680330) | Cod sursa (job #2450614) | Cod sursa (job #2078748) | Cod sursa (job #553598)
Cod sursa(job #553598)
#include<stdio.h>
#define dim 16005
using namespace std;
int i,n,x,y,rad,cost[dim],mat[dim][dim],viz[dim];
long long max=-999999999,sum;
void dfs(int x)
{int i,nod;
viz[x]=1;
for(i=1; i<=mat[x][0]; i++)
if(!viz[mat[x][i]])
{nod=mat[x][i];
if(cost[nod]+sum >= 0) {sum=cost[nod]+sum; if(sum>max) max=sum;}
else sum=0;
dfs(nod);
}
}
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]); if(cost[i]>max) {rad=i; max=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;
}
sum=cost[rad]; dfs(rad);
fprintf(g,"%lld\n",max);
fclose(f);
fclose(g);
return 0;
}