Cod sursa(job #2329230)

Utilizator mihailescu_eduardMihailescu Eduard-Florin mihailescu_eduard Data 26 ianuarie 2019 14:55:40
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

static const int NMAX  = 16005;
int n;
vector<int> v[NMAX];
vector<int> stack;
int val[NMAX];
int valMax;
bool visited[NMAX];

void Solve(int node)
{
    visited[node] = true;
    stack.push_back(node);
    if(v[node].empty())
        valMax = max(valMax, val[node]);
    else{
        for(auto child : v[node])
        {
            if(!visited[child]){
                Solve(child);
                val[node] = max(val[node], val[node] + val[stack.back()]); 
                valMax = max(valMax,val[node]);
                stack.pop_back();
            }
        }
    }
}

int main()
{
    fin >> n;
    for(int i =1; i<= n; ++i)
    {
        fin >> val[i];
    }
    int x, y;
    for(int i = 1; i< n; ++i)
    {
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    valMax= val[1];
    Solve(1);
    fout << valMax;
    return 0;
}