Cod sursa(job #2824399)

Utilizator alexbrinzaAlexandru Brinza alexbrinza Data 2 ianuarie 2022 01:47:40
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int n, x, y, s;
vector < vector < int > > graph;
vector < int > vis, val;

int sum(int node, int &smax)
{
    int summ = val[node];
    vis[node] = 1;

    for(int i = 0; i < graph[node].size(); ++i)
    {
        int nnode = graph[node][i];

        if(vis[nnode] == 0)
        {
            int res = sum(nnode, smax);
            if(summ + res > summ) summ = summ + res;
        }
    }

    if(summ > smax) smax = summ;

    return summ;
}

void solve()
{
    in >> n;

    graph.resize(n + 1);
    vis.resize(n + 1, 0);
    val.resize(n + 1);

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

    for(int i = 1; i < n; ++i)
    {
        in >> x >> y;

        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    s = -1005;

    int res = sum(1, s);

    out << s;
}

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