Cod sursa(job #2837762)

Utilizator Stefan_GhinescuGhinescu Stefan-George Stefan_Ghinescu Data 22 ianuarie 2022 15:15:51
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>

std::ifstream fin("asmax.in");
std::ofstream fout("asmax.out");

const int NMAX = 16000;

int cost[NMAX + 5];
long long sum[NMAX + 5];

std::vector <int>G[NMAX + 5];
bool viz[NMAX + 5];

long long dfs(int x)
{
    long long s = cost[x];
    viz[x] = 1;
    for (auto it : G[x])
    {
        if (!viz[it])
        {
            s += std::max(dfs(it), 0LL);
        }
    }
    sum[x] = s;
    return s;
}

int main()
{
    int n, x, y;
    fin >> n;
    for (int i = 1; i <= n; ++i)
    {
        fin >> cost[i];
    }
    for (int i = 1; i < n; ++i)
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    x = dfs(1);
    for (int i = 1; i <= n; ++i)
    {
        if (sum[i] > x)
        {
            x = sum[i];
        }
    }
    fout << x;
    return 0;
}