Cod sursa(job #2408540)

Utilizator CronosClausCarare Claudiu CronosClaus Data 18 aprilie 2019 09:15:05
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

const int mxn = 16 * 1000 + 10;

vector< int > g[ mxn ];

int val[ mxn ];

int dp[ mxn ];

bitset< mxn > viz;

int n;

int sol = -1e5;

void dfs(int nod){
    viz[ nod ] = 1;
    for(auto it: g[ nod ]){
        if(viz[ it ] == 0){
            dfs( it );
            dp[ nod ] += max(0, dp[ it ]);
        }
    }
    dp[ nod ] += val[ nod ];
    sol = max(sol, dp[ nod ]);
}

int main()
{
    ifstream cin("asmax.in");
    ofstream cout("asmax.out");
    cin>> n;
    for(int i = 1; i <= n; i++){
        cin>> val[ i ];
        sol = max(sol, val[ i ]);
    }
    for(int i = 1, x, y; i < n; i++){
        cin>> x >> y;
        g[ x ].push_back( y );
        g[ y ].push_back( x );
    }
    dfs( 1 );
    cout<< sol;

    return 0;
}