Cod sursa(job #2540986)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 7 februarie 2020 21:58:00
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int MAXN = 16010, MIN = -1010;

int dp[MAXN], res = MIN, n;
vector<int> graph[MAXN];

void read()
{
    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> dp[i];
    for (int i = 0; i < n - 1; ++i) {
        int x, y;
        fin >> x >> y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
}

void dfs(int node, int fat)
{
    for (const auto& it: graph[node])
        if (it != fat) {
            dfs(it , node);
            if (dp[it] > 0)
                dp[node] += dp[it];
        }
    res = max(res, dp[node]);
}

int main()
{
    read();
    dfs(1, 1);
    fout << res << '\n';
    return 0;
}