Cod sursa(job #624324)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 22 octombrie 2011 11:08:25
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <vector>
using namespace std;

int n, sol = -2000000000;
int v[16100], ap[16100], cost[16100];
vector <int> A[16100];

void DFs(int nod)
{
    ap[nod] = 1;
    cost[nod] = v[nod];
    for (vector <int> :: iterator it = A[nod].begin(); it != A[nod].end(); ++it)
    {
        if (!ap[*it])
        {
            DFs(*it);
            if (cost[*it] > 0) cost[nod] += cost[*it];
        }
    }
}
int main()
{
    ifstream f("asmax.in");
    ofstream g("asmax.out");

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

    DFs(1);
    for (int i = 1; i <= n; ++i) sol = max(sol, cost[i]);

    g << sol << '\n';
    g.close();
    return 0;
}