Cod sursa(job #2535019)

Utilizator xCata02Catalin Brita xCata02 Data 31 ianuarie 2020 12:13:55
Problema Asmax Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;

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

#define cin fin
#define cout fout

#define Nmax 16001

int n;
int cost[Nmax];
int dp[Nmax];

int Max = -INT_MAX;

vector < int > g[Nmax];

bitset < Nmax > viz;

void read()
{
    int x, y;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> cost[i];
    }

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

void DFS(int nod)
{
    int x;
    viz[nod] = 1;
    for(const auto it : g[nod])
    {
        if(viz[it] == 0)
        {
            x = it;
            viz[it] = 1;
            dp[nod] = max(dp[it-1] + cost[it], dp[it]);
            DFS(it);
        }
    }
    Max = max(dp[nod], Max);
}

void solve()
{
    DFS(1);
    cout << Max;
}

int main()
{
    read();
    solve();
}