Cod sursa(job #277599)

Utilizator Mishu91Andrei Misarca Mishu91 Data 11 martie 2009 20:02:05
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
//e scrisa in borland :-&
#include <stdio.h>

#define MAX_N 16005
#define INF 0x3f3f3f

int N, *V, *S;
long Max = -INF;

typedef struct nod
{
	int x;
	nod *next;
}*list;

list G[MAX_N];

void add(list &A, int val)
{
	list t = new nod;
	t -> x = val;
	t -> next = A;
	A = t;
}

void citire()
{
	scanf("%d",&N);
	V = new int [N+1];
	S = new int [N+1];
	int i;

	for(i = 1; i <= N; ++i)
		scanf("%d",V+i);

	for(i = 1; i < N; ++i)
	{
		int x, y;
		scanf("%d %d",&x, &y);
		add(G[x], y);
		add(G[y], x);
	}
}

void df(int nod, int tata)
{
	S[nod] = V[nod];

	for(list p = G[nod]; p; p = p -> next)
	{
		if(p -> x == tata) continue;
		df(p -> x, nod);
		if(S[p -> x] > 0)
			S[nod] += S[p -> x];
	}

	if(S[nod] > Max)
		Max = S[nod];
}

int main()
{
	freopen("asmax.in","rt",stdin);
	freopen("asmax.out","wt",stdout);

	citire();
	df(1, 0);
	printf("%d\n",Max);
	return 0;
}