Cod sursa(job #2603703)

Utilizator bmc213Mihai Cosmin bmc213 Data 20 aprilie 2020 18:11:30
Problema Asmax Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>

std::ifstream f("asmax.in");
std::ofstream g("asmax.out");

std::vector <int> v[16001];
bool viz[16001];
int n, m, t, i, x, y;
long long a[16001], smax = -16000000001;

void dfs(int nod)
{
    viz[nod] = 1;
    for(std::vector<int>::iterator it = v[nod].begin(); it != v[nod].end(); ++ it)
    {
        if(!viz[*it])
        {
            a[nod] = a[nod] + a[*it];
            dfs(*it);
        }
    }
}
int main()
{
    f >> n;
    for(i = 1; i <= n; ++ i)
    {
        f >> a[i];
    }
    for(i = 1; i < n; ++ i)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);
    for(i = 1; i <= n; ++ i)
    {
        if(a[i] > smax)
            smax = a[i];
    }
    g << smax << "\n";

    return 0;
}