Cod sursa(job #3304944)

Utilizator razvanmrt_06Mariuta Razvan razvanmrt_06 Data 28 iulie 2025 21:12:36
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>

using namespace std;

const int Nmax = 16005;

int n, val[Nmax], smaxp[Nmax];
vector<int> v[Nmax];

void dfs(int father, int son){
    smaxp[son] = val[son];
    for(int nod : v[son]){
        if(nod != father){
            dfs(son, nod);
            if(smaxp[nod] > 0){
                smaxp[son] += smaxp[nod];
            }
        }
    }
}

int main()
{
    ifstream fin("asmax.in");
    ofstream fout("asmax.out");
    fin >> n;
    for(int i = 1; i <= n; i++){
        fin >> val[i];
    }
    for(int i = 1; i <= n-1; i++){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, 1);
    int sol = INT_MIN;
    for(int i = 1; i <= n; i++){
        sol = max(sol, smaxp[i]);
    }
    fout << sol;

    return 0;
}