Cod sursa(job #50970)

Utilizator amadaeusLucian Boca amadaeus Data 9 aprilie 2007 14:42:52
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdio>
#include <vector>

using namespace std;

typedef vector< int > vi;
typedef vi::iterator vit;

#define NX 16010
#define INF 0x3f3f3f3f

#define tr(X,i) \
	for( vit i = X.begin(); i != X.end(); i++ )

vi G[ NX ];
bool col[ NX ];
int N, V[ NX ], c[ NX ], res;

void cit() {
	int i, u, v;
	
	scanf( "%d", &N );

	for( i = 1; i <= N; i++ )
		scanf( "%d", V + i );
	for( i = 1; i <= N; i++ ) {
		scanf( "%d%d", &u, &v );
		G[u].push_back( v );
		G[v].push_back( u );
	}
}

inline void umax( int& x, int y ) {
	if( x < y )
		x = y;
}

int DFS( int u ) {
	col[ u ] = 1;
	c[ u ] = V[ u ];

	tr( G[u], v )
		if( col[ *v ] == 0 ) {
			int val = DFS( *v );
			if( val > 0 )
				c[ u ] += val;
		}
	umax( res, c[u] );
	return c[u];
}

void rez() {
	res = -INF;
	DFS( 1 );
}

void scr() {
	printf( "%d\n", res );
}

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

	cit();
	rez();
	scr();

	return 0;
}