Cod sursa(job #2490443)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 10 noiembrie 2019 12:06:58
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>
#define NMAX 16001
using namespace std;

ifstream fin{"asmax.in"};
ofstream fout{"asmax.out"};

vector<vector<int>> arbore(NMAX, vector<int>());

int N, V[NMAX], ans = -2000000000;


int dpDFS(int nod, int tata)
{
    int s_max_nod = V[nod];

    for(auto& fiu : arbore[nod])
    {
        if(fiu == tata) continue;

        int s_max_fiu = dpDFS(fiu, nod);

        s_max_nod += max(0, s_max_fiu);
    }

    ans = max(ans, s_max_nod);

    return s_max_nod;
}


int main()
{
    fin >> N;

    for(int i = 1; i <= N; ++i)
    {
        fin >> V[i];
    }

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

    dpDFS(1, -1);

    fout << ans;
}