Cod sursa(job #2909983)

Utilizator francescom_481francesco martinut francescom_481 Data 17 iunie 2022 13:56:08
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define cin fin
#define cout fout

#define N 16005
int f[N], c[N], v[N], n, x, y, cost, pct, rez;
vector < vector < int > > g;

int dfs(int nod)
{
    f[nod] = 1;
    c[nod] = v[nod];
    for(unsigned int z = 0; z < g[nod].size(); ++z) {
        if(f[g[nod][z]] == 0) {
            int r = dfs(g[nod][z]);
            c[nod] = max(c[nod] + r, c[nod]);
        }
    }
    return c[nod];
}

int main()
{
    cin >> n;
    g.resize(n+5);
    pct = 1, cost = -1005;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> v[i];
        if(v[i] > cost)
        {
            cost = v[i];
            pct = i;
        }
    }
    for(int i = 1 ; i < n ; i++)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(pct);
    for(int i = 1 ; i <= n ; i++)
    {
        rez = max(rez,c[i]);
    }
    cout << rez;
    return 0;
}