Cod sursa(job #3000905)

Utilizator andreipirjol5Andrei Pirjol andreipirjol5 Data 13 martie 2023 08:35:28
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>
#include <cstring>

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

const int NMAX = 16000;
int d[NMAX + 5], father[NMAX + 5];
vector <int> graph[NMAX + 5];
int dp[NMAX + 5];

void get_father(int node)
{
    dp[node] = d[node];
    for(auto neigh : graph[node])
        if(neigh != father[node])
        {
            father[neigh] = node;
            get_father(neigh);

            if(dp[neigh] >= 0)
                dp[node] += dp[neigh];
        }
}

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

    for(int i = 1; i <= n - 1; i++)
    {
        int a, b;
        fin >> a >> b;

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    get_father(1);

    int maxim = 0;

    for(int i = 1; i <= n; i++)
        maxim = max(maxim , dp[i]);

    fout << maxim;

    fin.close();
    fout.close();
    return 0;
}