Pagini recente » Cod sursa (job #541302) | Cod sursa (job #505938) | Cod sursa (job #1535464) | Cod sursa (job #1533769) | Cod sursa (job #644013)
Cod sursa(job #644013)
# include <fstream>
# include <vector>
# define dim 16005
# define pb push_back
using namespace std;
int n, sol = -999999;
int v[ dim ], viz[ dim ], cost[ dim ];
vector <int> a[ dim ];
ifstream f("asmax.in");
ofstream g("asmax.out");
void dfs( int nod )
{
int i;
viz[ nod ] = 1;
cost[ nod ] = v[ nod ];
for ( i = 0 ; i < a[ nod ].size() ; i++ )
if ( viz[ a[ nod ][ i ] ] == 0)
{
dfs( a[ nod ][ i ] );
if ( cost[ a[ nod ][ i ] ] > 0 )
cost[ nod ] += cost[ a[ nod ][ i ] ];
}
}
void citire()
{
int i, x, y;
f >> n;
for ( i = 1; i <= n; ++i)
f >> v[i];
for ( i = 1; i < n; ++i)
{
f >> x >> y;
a[ x ].pb(y);
a[ y ].pb(x);
}
}
void rezolva()
{
int i;
dfs(1);
for ( i = 1; i <= n; ++i )
sol = max(sol, cost[i]);
}
void afisare()
{
g << sol << "\n";
}
int main()
{
citire();
rezolva();
afisare();
return 0;
}