Cod sursa(job #583720)

Utilizator vlad2901Vlad Berindei vlad2901 Data 22 aprilie 2011 10:16:35
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <stdio.h>
#define NMAX 16000


char viz[NMAX];
long long best[NMAX];
short val[NMAX];

struct nod {
	short info;
	nod *next;
}*v[NMAX];

void df(short);

int main() {
	
	int n,i,x,y;
	nod *c;
	long long max;
	
	freopen("asmax.in", "r", stdin);
	freopen("asmax.out", "w", stdout);
	
	scanf("%d", &n);
	
	for(i=1;i<=n;i++) {
		scanf("%d", &val[i]);
		best[i] = val[i];
	}
			
	
	for(i=0;i<n-1;i++) {
	
		scanf("%d", &x);
		scanf("%d", &y);
		c = new nod;
		c->info = y;
		c->next = v[x];
		v[x] = c;

		c = new nod;
		c->info = x;
		c->next = v[y];
		v[y] = c;			
	}
	
	df(1);
	
	max = 0;
	
	for(i=1;i<=n;i++) {
		if(max < best[i]) {
			max = best[i];
		}
	}
	
	printf("%d", max);	
	
	return 0;
}

void df(short x) {
	nod *c;
	short curent;
	long long rez;
	viz[x] = 1;
	for(c = v[x]; c; c=c->next) {
		if(c && !viz[c->info]) {
			curent = c->info;			
			df(curent);	
			//rez = best[curent];
			//if(rez == -1001) {
			//	rez = val[curent];
			//}
			if(best[curent] > 0)	
				best[x] += best[curent];
		}
	}	
}