Pagini recente » Cod sursa (job #153890) | Cod sursa (job #1521263) | Cod sursa (job #1237377) | Cod sursa (job #2871356) | Cod sursa (job #50970)
Cod sursa(job #50970)
#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;
}