Cod sursa(job #2856877)

Utilizator marateodorescu11Teodorescu Mara marateodorescu11 Data 24 februarie 2022 15:00:55
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("asmax.in");
ofstream fout ("asmax.out");
const int N = 16001;
vector <int> a[N];
bool viz[N];
int n, suma[N], val[N];
void citire ()
{
    fin >> n;
    for (int i=1; i<=n; i++)
        fin >> val[i];
    for (int i=1; i< n; i++){
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);

    }

}
void dfs (int x)
{
    viz[x] = 1;
    suma[x] = val[x];
    for (auto y: a[x]){
        if (!viz[y]){
            dfs(y);
            if (suma[y] > 0)
                suma[x] += suma[y];
        }

    }
}
int main()
{
    citire ();
    dfs(1);
    int maxi = -1e7;
    for (int i=1; i<=n; i++)
        if (suma[i] > maxi)
            maxi = suma[i];
    fout << maxi;
    return 0;
}