Cod sursa(job #2429061)

Utilizator viftode4Iftode Vlad viftode4 Data 7 iunie 2019 15:53:11
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin( "asmax.in" );
ofstream fout( "asmax.out" );
int n, x, y;
int sol = -1 * INT_MAX;
vector<int>g[16005];
int cost[16005];
int use[16005];
int dp[16005];
void dfs( int x )
{
    use[x] = 1;

    for( auto it : g[x] )
        if( !use[it] )
            {
                dfs( it );

                if( dp[it] > 0 )
                    dp[x] += dp[it];
            }
}
int main()
{
    fin >> n;

    for( int i = 1; i <= n; i++ )
        {
            fin >> cost[i];
            dp[i] = cost[i];
        }

    for( int i = 1; i < n; i++ )
        {
            fin >> x >> y;
            g[x].push_back( y );
            g[y].push_back( x );
        }

    dfs( 1 );

    for( int i = 1; i <= n; i++ )
        sol = max( sol, dp[i] );

    fout << sol;
    return 0;
}