Cod sursa(job #3326397)

Utilizator geo_uwuManolachi George geo_uwu Data 28 noiembrie 2025 17:51:16
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int maxi = -__INT_MAX__;

int dfs(int x, int xx, vector<vector<int>> &a, vector<int> &w)
{
    for (int y : a[x])
    {
        if (y != xx)
        {
            int rez = dfs(y, x, a, w);
            if (rez > 0)
            {
                w[x] += rez;
            }
        }
    }

    if (w[x] > maxi)
        maxi = w[x];

    return w[x];
}

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

    int n, wi, x, y;

    f >> n;

    vector<vector<int>> a(n + 1);
    vector<int> w(n + 1);

    for (int i = 1; i <= n; i++)
    {
        f >> wi;
        w[i] = wi;
    }

    for (int i = 1; i < n; i++)
    {
        f >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    dfs(1, -1, a, w);

    // for (int i = 1; i <= n; i++)
    // {
    //     if (w[i] > maxi)
    //         maxi = w[i];
    // }

    g << maxi;

    f.close();
    g.close();
    return 0;
}