Cod sursa(job #2832836)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 14 ianuarie 2022 13:36:48
Problema Asmax Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

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

int dp[16006], n, v[16006], mx = -1005;
vector <int> h[16005];

int main()
{
    int x, y;
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> v[i];
    for(int i = 1; i < n; i++)
    {
        fin >> x >> y;
        h[x].push_back(y);
        h[y].push_back(x);
    }
    for(int i = n; i >= 1; i--)
    {
        dp[i] = v[i];
        for(auto j : h[i])
            if(dp[j] > 0)
                dp[i] += dp[j];
        ///-1 1 3 1 -1
        dp[i] = max(0, dp[i]);
    }

    for(int i = 1; i <= n; i++)
        mx = max(mx, dp[i]);
    fout << mx;
    return 0;
}