Cod sursa(job #3375519)

Utilizator LucaMirsolea14Luca Mirsolea LucaMirsolea14 Data 12 noiembrie 2026 10:48:55
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 16002;
int n, cost[N], fr[N], suma[N], ans=INT_MIN;

vector<int> g[N];

void dfs(int nod){

    fr[nod] = 1;
    suma[nod] = cost[nod];
    for(int e : g[nod])
        if(!fr[e]){
            dfs(e);
            suma[nod] += max(0, suma[e]);
        }
    ans = max(ans, suma[nod]);
}

int32_t main(){

    int i, x, y;

    fin >> n;

    for(i=1;i<=n;i++)
        fin >> cost[i];

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

    dfs(1);

    fout << ans;

}