Cod sursa(job #2824778)

Utilizator MihaiZ777MihaiZ MihaiZ777 Data 3 ianuarie 2022 15:08:55
Problema Asmax Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int n;
vector <int> graph[16005];
int val[16005];
bool visited[16005];

int bkt(int node, int oldNode)
{
    if (visited[node] == true)
    {
        return 0;
    }

    int sum = val[node];
    visited[node] = true;
    for (int x : graph[node])
    {
        if (x == oldNode)
        {
            continue;
        }

        int newVal = bkt(x, node);
        if (newVal > 0)
        {
            sum += newVal;
        }
    }

    return sum;
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
    {
        fin >> val[i];
    }

    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        fin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    fout << bkt(1, 0) << '\n';
}