Cod sursa(job #2856885)

Utilizator TeoRoGaming_YgVoinea Ionut-Florin TeoRoGaming_Yg Data 24 februarie 2022 15:23:20
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int N = 16001;

vector <int> a[N];

bool viz[N];

int n, suma[N], val[N];

void citire ()
{
    int i, x, y;
    f >> n;
    for (i=1; i<=n; i++)
        f >> val[i];
    for (i=1; i< n; i++)
    {
        f >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
}
void dfs (int x)
{
    viz[x] = 1;
    suma[x] = val[x];
    for (auto y: a[x])
    {
        if (!viz[y])
        {
            dfs(y);
            if (suma[y] > 0)
                suma[x] += suma[y];
        }
    }
}
int main()
{
    int i;
    citire();
    dfs(1);
    int maxim = -1e7;
    for (i=1; i<=n; i++)
        if (suma[i] > maxim)
            maxim = suma[i];
    g << maxim;
    return 0;
}