Cod sursa(job #150)

Utilizator webspiderDumitru Bogdan webspider Data 5 decembrie 2006 22:06:40
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

vector<int> fii[16001];

int cost[16001];
long long costt[16001];
bool mark[16001];
int n,m;
long long total=-100000000;

int dfs1( int nodc )
{
	int l;
	mark[ nodc ] = 1;
	for ( l=0; l< fii[nodc].size(); l++ )
		if ( !mark[ fii[nodc][l] ] )
		{
			dfs1( fii[nodc][l] );
		}
		else
			fii[nodc][l] = 0;
}

int dfs2( int nodc )
{
	int l;
	long long x = cost[nodc];
	for ( l=0; l< fii[nodc].size(); l++ )
		if ( fii[nodc][l] )
			x+=dfs2( fii[nodc][l] );
	costt[ nodc ] = x;
	if ( costt[nodc] < 0 ) return 0;
	return x;	
}



int main()
{
	int a,b;
	int i;
	freopen("asmax.in","r",stdin);
	freopen("asmax.out","w",stdout);
	
	scanf("%d\n", &n );
	
	for (i=1; i<=n; i++)
	{
		scanf("%d ", &cost[i] );
	}
	
	for (i=1; i<n; i++)
	{
		scanf("%d %d ", &a, &b );
		fii[a].push_back( b );
		fii[b].push_back( a );
	}
	dfs1( 1 );
	dfs2( 1 );
	for ( i=1; i<=n; i++ ) total = max( total, costt[i] );

	printf("%d\n", total );
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}