Cod sursa(job #2916744)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 1 august 2022 12:12:43
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <bitset>
#include <vector>

using namespace std;

ifstream cin("asmax.in");
ofstream cout("asmax.out");

const int NMAX = 16e3;

int v[NMAX + 1], dp[NMAX + 1], n, ans = -1e9;
vector <int> g[NMAX + 1];
bitset <NMAX + 1> vizitat;

void dfs(int x){

    vizitat[x] = 1;
    int suma_max = v[x];

    for(auto y : g[x]){

        if(!vizitat[y]){

            dfs(y);

            if(suma_max < suma_max + dp[y])
                suma_max += dp[y];
        }

    }

    dp[x] = suma_max;
    ans = max(ans, dp[x]);
    //cout << "suma maxima dintr un subarbore cu radacina in " << x << " este = " << dp[x] << "\n";

}

int main(){

    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> v[i];

    int x = 0, y = 0;
    for(int i = 0; i < n - 1; i++){

        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);

    }

    dfs(1);
    cout << ans;

    return 0;
}