Cod sursa(job #1351036)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 21 februarie 2015 09:23:07
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include    <iostream>
#include    <fstream>
#include    <vector>

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

const int NMax = 16005;
const int oo =  -(1<<30);

vector < int > V[NMax];

int N, Values[NMax], Sum[NMax], Result = -(1<<30);
bool beenThere[NMax];

void DFS(int node)
{
    beenThere[node] = true;
    Sum[node] = Values[node];

    for(unsigned int i = 0; i < V[node].size(); i++)
    {
        int Neighbor = V[node][i];
        if(!beenThere[Neighbor])
        {
            DFS(Neighbor);
            Sum[node] = max(Sum[node], Sum[node] + Sum[Neighbor]);
        }
    }

    Result = max(Result, Sum[node]);
}
void Read()
{
    int N;
    fin >> N;

    for(int i = 1; i <= N; i++)
        fin >> Values[i];

    for(int i = 1; i < N; i++)
    {
        int x, y;
        fin >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }

    DFS(1);
    fout << Result;
}

int main()
{
    Read();
    return 0;
}