Cod sursa(job #536100)

Utilizator cosmin79Carabet Cosmin Andrei cosmin79 Data 18 februarie 2011 11:32:26
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <stdio.h>
#include <vector>
#define NMAX 16005
#define pb push_back
using namespace std;
int n,A[NMAX],S[NMAX],rez;
vector <int> B[NMAX];
char viz[NMAX];
void read()
{
	scanf("%d",&n);
	int i,x,y;
	for (i=1; i<=n; i++)
		scanf("%d",&A[i]);
	for (i=1; i<n; i++)
	{
		scanf("%d%d",&x,&y);
		B[x].pb(y); B[y].pb(x);
	}
}
void dfs(int nod)
{
	viz[nod]=1; S[nod]=A[nod];
	int i,vec;
	for (i=0; i<B[nod].size(); i++)
	{
		vec=B[nod][i];
		if (!viz[vec])
		{
			dfs(vec);
			if (S[nod]<S[nod]+S[vec])
				S[nod]=S[nod]+S[vec];
		}
	}
	if (S[nod]>rez) 
		rez=S[nod];
}
int main()
{
	freopen("asmax.in","r",stdin);
	freopen("asmax.out","w",stdout);
	read();
	dfs(1);
	printf("%d\n",rez);
	return 0;
}