Cod sursa(job #2291830)

Utilizator stefan.botezStefan Botez stefan.botez Data 28 noiembrie 2018 17:56:11
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <cstdio>
#include <vector>

using namespace std;

int n, val[16002], dp[16002], maxim;
vector<int> v[16002];
bool viz[16002];

void dfs(int x)
{
    viz[x] = 1;

    dp[x] = val[x];

    for (int y : v[x])
    {
        if (!viz[y])
        {
            dfs(y);
            if(dp[y] > 0)
                dp[x] += dp[y];
        }
    }
}

int main()
{
    int x, y;

    freopen("asmax.in", "r", stdin);
    freopen("asmax.out", "w", stdout);

    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
        scanf("%d", &val[i]);

    for (int i = 1; i <= n - 1; i++)
    {
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
    }

    dfs(1);

    maxim = dp[1];
    for(int i = 2; i <= n; i++)
        if(maxim < dp[i])
            maxim = dp[i];

    printf("%d\n", maxim);
    return 0;
}