Cod sursa(job #3000895)

Utilizator andreipirjol5Andrei Pirjol andreipirjol5 Data 13 martie 2023 08:30:25
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 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];
bool fr[NMAX + 5];

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

void dfs(int node)
{
    if(father[node] == 0)
        return;

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

    node = father[node];
}

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);

    for(int i = 1; i <= n; i++)
        fr[father[i]] = 1;

    for(int i = 1; i <= n; i++)
        if(fr[i] == 0)
            dp[i] = d[i];

    for(int i = 1; i <= n; i++)
        dfs(i);

    int maxim = 0;

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

    fout << maxim;

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