Cod sursa(job #2677991)

Utilizator FrostfireMagirescu Tudor Frostfire Data 27 noiembrie 2020 21:54:27
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 16000
#define inf 1001

using namespace std;

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

int n, v[NMAX+10], dp[NMAX+10], ans;
vector <int> nod[NMAX+10];

void dfs(int x, int p)
{   for(int i=0; i<nod[x].size(); i++)
        if(nod[x][i] != p)
            {   dfs(nod[x][i], x);
                dp[x] += max(0, dp[nod[x][i]]);
            }
    dp[x] = max(0, v[x] + dp[x]);
    ans = max(ans, dp[x]);
}

int main()
{
    f >> n;
    int maxi = -inf, ok = 0;
    for(int i=1; i<=n; i++)
        {   f >> v[i];
            if(v[i] >= 0) ok = 1;
            maxi = max(maxi, v[i]);
        }
    if(!ok)
        {   g << maxi << '\n';
            return 0;
        }
    for(int i=1; i<n; i++)
        {   int nod1, nod2;
            f >> nod1 >> nod2;
            nod[nod1].push_back(nod2);
            nod[nod2].push_back(nod1);
        }
    dfs(1, 0);
    g << ans << '\n';
    return 0;
}