Cod sursa(job #1842022)

Utilizator alexandruchiriacAlexandru Chiriac alexandruchiriac Data 6 ianuarie 2017 14:01:39
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream f("asmax.in");
ofstream g("asmax.out");

int n,m,x,y;
int v[16001];
bool used[16001];
long long best[16001];
vector < int > G[16001];

void dfs ( int i )
{
    used[i] = 1;
    for ( vector < int > ::iterator j = G[i].begin(); j != G[i].end(); j++ )
    {
        if( !used[ (*j) ] )
        {
            dfs( (*j) );
            if ( best[(*j)] > 0 ) best[i] = best[i] + best[(*j)];
        }
    }
}

int main()
{
    f >> n;
    for ( int i = 1; i <= n ; i++ ) f >> v[i] , best[i] = v[i];
    int m = n-1;
    for ( ; m-- ; )
    {
        f >> x >> y ;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    long long amax = best[1];
    for ( int i = 2 ; i <= n ; i++ )
        if ( amax < best[i]) amax = best[i];
    g << amax;
    return 0;
}