Cod sursa(job #3340033)

Utilizator fei_runeFei Rune fei_rune Data 11 februarie 2026 20:30:42
Problema Asmax Scor 60
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 Marime 0.87 kb
#include <fstream>
#include <vector>
#include <climits>

using namespace std;

const int Nmax = 16005;

int n;
int max_sum[Nmax], val[Nmax];
vector<int> V[Nmax];

void dfs(int node, int father) {
    max_sum[node] = val[node];
    for(int son : V[node]) {
        if(son != father) {
            dfs(son, node);
            if(max_sum[son] > 0) {
                max_sum[node] += max_sum[son];
            }
        }
    }
}

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; i++) {
        int x, y;
        fin >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }
    
    dfs (1, 0);
    
    int sol = INT_MIN;
    
    for(int i = 1; i <= n; i++) {
        sol = max(sol, max_sum[i]);
    }
        
    fout << sol;

    return 0;
}