Cod sursa(job #3157998)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 17 octombrie 2023 16:32:11
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda HLO 2023 - Lot - Tema 0 Marime 0.86 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int max_size = 16e3 + 1;

int viz[max_size], dp[max_size], ans;

vector <int> edges[max_size];

void dfs (int nod)
{
    viz[nod] = 1;
    for (auto f : edges[nod])
    {
        if (viz[f] == 0)
        {
            dfs(f);
            if (dp[f] > 0)
            {
                dp[nod] += dp[f];
            }
        }
    }
    ans = max(dp[nod], ans);
}

int main ()
{
    int n;
    in >> n;
    for (int i = 1; i <= n; i++)
    {
        in >> dp[i];
    }
    for (int i = 1; i < n; i++)
    {
        int x, y;
        in >> x >> y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    ans = dp[1];
    dfs(1);
    out << ans;
    in.close();
    out.close();
    return 0;
}