Cod sursa(job #3229357)

Utilizator addanciuAdriana Danciu addanciu Data 15 mai 2024 16:09:03
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

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

const int N = 16000;

int val[N + 1];
int nrd[N + 1]; //nr de descendenti
bool viz[N + 1];
vector<int>a[N + 1];

int maxx = -1001;

void dfs(int x){
    viz[x] = 1;
    nrd[x] = 1;
    for(auto y : a[x]){
        if(!viz[y]){
            dfs(y);
            nrd[x] += nrd[y];
            val[x] = max(val[x], val[x] + val[y]);
        }
    }
    maxx = max(maxx, val[x]);
}

int main(){
    int n, x, y;
    in >> n;
    for(int i = 1; i <= n; i++) in >> val[i];
    for(int i = 1; i <= n - 1; i++){
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1);
    out << maxx;
    return 0;
}