Cod sursa(job #3210609)

Utilizator xxUnrealUxxNiculae Adrian-Ioan xxUnrealUxx Data 6 martie 2024 20:52:33
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>
#include <climits>
#define Nmax 16010
using namespace std;

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

vector<int> Graph[Nmax];
vector<int> mat(Nmax, 0);
vector<int> dp(Nmax, 0);
int n;

void DFS (int node, int father) 
{
    for (const int& next : Graph[node]) 
    {
        if (next != father)
        {
            DFS (next, node);
            if (dp[next] > 0) dp[node] += dp[next];
        }
    }
}

int main()
{
    cin >> n;
    for(int i = 1; i<=n; i++)
    {
        cin >> mat[i];
        dp[i] = mat[i];
    }

    for(int i = 1, a, b; i<n; i++)
    {
        cin >> a >> b;
        Graph[a].push_back(b);
        Graph[b].push_back(a);
    }
    DFS(1, 0);

    int maxx = INT_MIN;

    for(int i = 1; i<=n; i++)
    {
        maxx = max(maxx, dp[i]);
    }
    cout << maxx;
}