Cod sursa(job #3230484)

Utilizator PetrudpPetru Paun Petrudp Data 21 mai 2024 19:02:58
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream citire("asmax.in");
ofstream scrie("asmax.out");


const int N = 16000;

int vec[N + 1], valMax[N + 1];
bool vazut[N + 1];
vector<int> a[N + 1];

int n;

void SetValMax(int x)
{
    vazut[x] = true;
    valMax[x] = vec[x];
    for(auto y: a[x])
    {
        if(!vazut[y])
        {
            SetValMax(y);
            if(valMax[y] > 0)
            {
                valMax[x] += valMax[y];
            }
        }
    }
}

void CitireVec()
{
    citire >> n;
    for(int i = 1; i <= n; i++)
    {
        citire >> vec[i];
    }
}

void CitireLeg()
{
    for(int i = 0; i < n - 1; i++)
    {
        int nr1, nr2;
        citire >> nr1 >> nr2;
        a[nr1].push_back(nr2);
        a[nr2].push_back(nr1);
    }
}

int main()
{
    CitireVec();
    CitireLeg();
    SetValMax(1);

    int maxv = -1000;
    for(int i = 1; i <= n; i++)
    {
        maxv = max(maxv, valMax[i]);
    }
    scrie << maxv;

    citire.close();
    scrie.close();
    return 0;
}