Cod sursa(job #3283969)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 10 martie 2025 19:25:39
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("asmax.in");
ofstream g ("asmax.out");

const int NMAX = 16e3;

int dp[NMAX + 1];
vector<int> adj[NMAX + 1];
bool marked[NMAX + 1];
int v[NMAX + 1];

void dfs(int nod){
    
    marked[nod] = true;
    dp[nod] = v[nod]; //are minim propria valoare
    
    for(int next : adj[nod]){
        
        if(!marked[next]){
            
            dfs(next);
            
            if(dp[next] > 0)
                dp[nod] += dp[next];
            
            
        }
        
    }
    
}

int main()
{
    
    int n;
    f >> n;
    
    for(int i=1; i<=n; i++)
        f >> v[i];
    
    for(int i=1; i<n; i++){
        
        int x, y;
        f >> x >> y;
        
        adj[x].push_back(y);
        adj[y].push_back(x);
        
    }
    
    dfs(1);
    
    int mx = -1e9;
    
    for(int i=1; i<=n; i++)
        mx = max(mx, dp[i]);
        
    g << mx;
    

    return 0;
}