Cod sursa(job #2836154)

Utilizator AndreiCroitoruAndrei Croitoru AndreiCroitoru Data 19 ianuarie 2022 20:47:38
Problema Asmax Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

vector<int> g[16001];
int n;
bool viz[16001];
int sum[16001], ans = 0;
int v[16001];
void dfs( int nod )
{
    viz[nod] = true;
    sum[nod] = v[nod];
    for( auto x: g[nod])
    {
        if (viz[x] == false)
        {
            dfs(x);
            if( sum[nod] + v[x] > sum[nod])
                sum[nod] += v[x];
        }
    }
    ans = max(ans, sum[nod]);
}

int main()
{

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

    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> v[i];
    }
    for(int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1);
    cout << ans;
    return 0;
}