Cod sursa(job #2591302)

Utilizator pregoliStana Andrei pregoli Data 30 martie 2020 12:15:43
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
#define newline '\n'
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
///***********************

const int NMAX = (1 << 4) * 1e3 + 5;
int n, a[NMAX], ans = -1e3 + 5;
vector<int> graph[NMAX];

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

void dfs(int node, int father)
{
    for (auto it : graph[node])
    {
        if (it != father)
        {
            dfs(it, node);
            if (a[it] > 0)
                a[node] += a[it];
        }
    }
    ans = max(ans, a[node]);
}

int main()
{
    read();
    dfs(1, 0);
    fout << ans << newline;
    fout.close();
    return 0;
}