Cod sursa(job #52156)

Utilizator alextheroTandrau Alexandru alexthero Data 17 aprilie 2007 23:08:13
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <stdio.h>
#include <vector>

#define pb push_back
#define nmax 16200
#define inf (int)1e9

using namespace std;

vector <int> e[nmax];
int c[nmax],x1,y1,i,n,a[nmax],v[nmax],maxim;

void dfs(int x) {
	v[x] = 1;
	a[x] = c[x];
	for(int i = 0; i < (int)e[x].size(); i++)
		if(!v[e[x][i]]) {
			dfs(e[x][i]);
			if(a[e[x][i]] > 0) a[x] += a[e[x][i]];
		}
	if(a[x] > maxim) maxim = a[x];
}

int main() {
	freopen("asmax.in","r",stdin);
	freopen("asmax.out","w",stdout);

	scanf("%d",&n);
	for(i = 1; i <= n; i++) scanf("%d",&c[i]);
	for(i = 1; i < n; i++) {
		scanf("%d%d",&x1,&y1);
		e[x1].pb(y1);
		e[y1].pb(x1);
	}

	maxim = -inf;
	dfs(1);
	printf("%d\n",maxim);

	return 0;
}