Cod sursa(job #569999)

Utilizator coderninuHasna Robert coderninu Data 2 aprilie 2011 13:19:03
Problema Asmax Scor 90
Compilator cpp Status done
Runda preselectie_acm_unibuc Marime 0.86 kb
#include <cstdio>
#include <list>
#define Nmax 16001

using namespace std;

list<int> G[Nmax];
int C[Nmax], N, rez;
int uz[Nmax];

int df(int x) {
	uz[x] = 1;
	if (G[x].size() == 1 && x != 1) {
		if (C[x] > rez) rez = C[x];
		return C[x];
	}
	
	int temp;
	temp = C[x];
	for (list<int>::iterator it = G[x].begin(); it != G[x].end(); ++it) {
		if (!uz[*it]) {
			int aux = df(*it);
			if (aux > 0) temp += aux;
		}
	}
	
	if (temp > rez) rez = temp;
	return temp;
}

int main() {
	freopen("asmax.in", "r", stdin);
	freopen("asmax.out", "w", stdout);
	
	scanf("%d\n", &N);
	for (int i = 1; i <= N; ++i ) scanf("%d ", C + i);
	for (int i = 0; i < N; ++i) {
		int x, y;
		scanf("%d %d\n", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	
	int temp = df(1);
	if (temp > rez) rez = temp;
	
	printf("%d\n", rez);
	
	return 0;
}