Cod sursa(job #3354051)

Utilizator cont_superscoalaSuperScoala cont_superscoala Data 14 mai 2026 15:00:41
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
/*
https://infoarena.ro/problema/asmax
*/
#include <fstream>
#include <vector>

using namespace std;

void dfs(int x, int t, vector <vector <int>> &a, vector <int> &val)
{
    for (auto y: a[x])
    {
        if (y != t)
        {
            dfs(y, x, a, val);
            if (val[y] > 0)
            {
                val[x] += val[y];
            }
        }
    }
}

int main()
{
    ifstream in("asmax.in");
    ofstream out("asmax.out");
    int n;
    in >> n;
    vector <int> val(n + 1);
    for (int i = 1; i <= n; i++)
    {
        in >> val[i];
    }
    vector <vector <int>> a(n + 1);
    for (int i = 0; i < n - 1; i++)
    {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    in.close();
    dfs(1, 0, a, val);
    int v_max = val[1];
    for (int i = 2; i <= n; i++)
    {
        v_max = max(v_max, val[i]);
    }
    out << v_max << "\n";
    out.close();
    return 0;
}