Pagini recente » Cod sursa (job #58899) | Cod sursa (job #537798) | Cod sursa (job #2105701) | Cod sursa (job #2349354) | Cod sursa (job #928407)
Cod sursa(job #928407)
#include<stdio.h>
#include <iostream>
#define dim 16005
using namespace std;
int i,n,p,x,j,y,rad,cost[dim],mat[dim][dim],viz[dim],nod[dim],best[dim];
long long Max=-99999999;
int sum = 0;
void DF(int x)
{int i;
viz[x] = 1;
for(i=1; i<=mat[x][0]; i++)
if(! viz[mat[x][i]] )
{
DF(mat[x][i]);
if(best[x] + best[mat[x][i]] > best[x])
{best[x] = best[x] + best[mat[x][i]];
if(best[x] > Max) Max = best[x];
}
}
}
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",&best[i]); if(best[i] > Max) Max = best[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;
}
DF(1);
fprintf(g,"%lld\n",Max);
fclose(f);
fclose(g);
return 0;
}