Cod sursa(job #3004152)

Utilizator tomaionutIDorando tomaionut Data 16 martie 2023 10:15:34
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, val[16005], dp[16005];
vector <int> a[16005];
bitset <16005> viz;

void Dfs(int x, int last)
{
    viz[x] = 1;
    dp[x] = val[x];
    for (auto w : a[x])
        if (viz[w] == 0)
        {
            Dfs(w, x);
            dp[x] = max(dp[x], dp[x] + dp[w]);
        }
}

int32_t main()
{
    int i, x, y, sol = -2e9;
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> val[i];
    for (i = 1; i < n; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    Dfs(1, 0);
    for (i = 1; i <= n; i++)
        sol = max(sol, dp[i]);
    fout << sol << "\n";

    return 0;
}