Cod sursa(job #1494371)

Utilizator crysstyanIacob Paul Cristian crysstyan Data 30 septembrie 2015 23:02:50
Problema Asmax Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define NMAX 16005

using namespace std;

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

int i, n, val[NMAX], leaf[NMAX], contleaf = 0, x, y, cont[NMAX], ans = -2000000000;
vector < int > v[NMAX];
bool visited[NMAX];

void read()
{
    f >> n;

    for (i = 1; i <= n; ++ i)
        f >> val[i];

    for (i = 1; i <= n - 1; ++ i)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
        cont[x] ++;
        cont[y] ++;
    }
}

void solve(int node)
{
    int s = 0;
    visited[node] = 1;

    if (v[node].empty())
        return;
    else
    {
        for (auto & it : v[node])
            if (!visited[it])
            solve(it);
        for (auto & it : v[node])
            s += val[it];
        val[node] += max(0, s);
    }
}

int main()
{
    read();
    solve(1);

    for (i = 1; i <= n; ++ i)
        ans = max(ans, val[i]);

    g << ans << '\n';
    return 0;
}