Cod sursa(job #1639987)

Utilizator secretCCMniciun nume secretCCM Data 8 martie 2016 15:11:35
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <vector>

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

const int Nmax = 16005;
int n, cost[Nmax], s = -1000000000, Max[Nmax];
vector <int> G[Nmax];

void DFS(int nod, int tata)
{
    for(int i = 0; i < (int)G[nod].size(); i++)
    {
        int vecin = G[nod][i];
        if(vecin!=tata)
        {
            DFS(vecin,nod);
        }

    }
    if(Max[nod] + cost[nod] > 0) Max[tata] += Max[nod] + cost[nod];
}

int main()
{
    f>>n;
    for(int i = 1; i <= n; i++) f>>cost[i];
    for(int i = 1; i < n; i++)
    {
        int x,y;
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1,0);
    for(int i = 1; i <= n; i++)
    {
        if(Max[i] + cost[i] > s) s = Max[i] + cost[i];
    }
    g<<s<<'\n';
    return 0;
}