Cod sursa(job #2706095)

Utilizator AndreiAlexandru2k3Ciucan Andrei Alexandru AndreiAlexandru2k3 Data 13 februarie 2021 19:34:33
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int NMAX = 16001;

vector<int>G[NMAX];
bool viz[NMAX];
int DP[NMAX], v[NMAX];
int N, maxx = -NMAX * 1000;

void citire()
{
    f >> N;
    for(int i = 1; i <= N; i++)
        f >> v[i];
    for(int i = 1; i < N; i++)
    {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void DFS(int nod)
{
    viz[nod] = 1;
    DP[nod] = v[nod];
    for(auto &x : G[nod])
        if(!viz[x])
        {
            DFS(x);
            DP[nod] += (DP[x] > 0)?DP[x]:0;
        }
    maxx = max(maxx, DP[nod]);
}

int main()
{
    citire();
    DFS(1);
    g << maxx;
    return 0;
}