Cod sursa(job #2681709)

Utilizator CRazvaN6Cutuliga Razvan CRazvaN6 Data 6 decembrie 2020 14:22:57
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>
using namespace std;

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

vector <int> g[16005];
int d[16005], v[16006];
bool check[16005];

int maxx,n;
void dfs(int nod)
{
    check[nod] = true;
    d[nod] = v[nod];
    for(auto y: g[nod])
    {
        if(check[y] == false)
        {
            dfs(y);
            if(d[y] >= 0 )
                d[nod] += d[y];
        }
    }
}
int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> v[i];
    for(int i = 2; i <= n; ++i)
    {
        int x,y;
        f >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    maxx = d[1];
    for(int i = 1; i <= n; ++i)
    {
        maxx = max(d[i], maxx);
    }
    out << maxx;
    return 0;
}