Cod sursa(job #220181)

Utilizator ProtomanAndrei Purice Protoman Data 9 noiembrie 2008 19:41:22
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <stdio.h>
#include <algorithm>
#define mx 1<<16

using namespace std;

struct nod
{
	int nr;
	nod *ua;
} *l[mx];
int i, n, x, y, maxim;
int sel[mx], nr[mx];

void clad(int t, int f)
{
	nod *p = new nod;
	p->nr = f;
	p->ua = l[t];
	l[t] = p;
}

int dfs(int x)
{
	sel[x] = 1;
	int s = nr[x];
	for (nod *p = l[x]; p; p = p->ua)
		if (!sel[p->nr])
		{
			int x = dfs(p->nr);
			if (x > 0)
				s += x;
		}
	maxim = max(maxim, s);
	return s;
}

int main()
{
	freopen("asmax.in","r",stdin);
	freopen("asmax.out","w",stdout);
	scanf("%ld", &n);
	for (int i = 1; i <= n; i++)
		scanf("%ld", &nr[i]);
	for (int i = 1; i < n; i++)
	{
		scanf("%ld %ld", &x, &y);
		clad(x, y);
		clad(y, x);
	}
	int x = dfs(1);
	printf("%ld\n", maxim);
	fclose(stdin);
	fclose(stdout);
	return 0;
}