Cod sursa(job #2567076)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 3 martie 2020 14:57:16
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>
#define nmax 16005
#define ll long long
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
bool ap[nmax];
int dp[nmax];
int n;
vector<int> lista[nmax];
void read(){
    in >> n;
    for(int i=1; i<=n; i++){
        in >> dp[i];
    }
    for(int i=1; i<n; i++){
        int a,b;
        in >> a >> b;
        lista[a].push_back(b);
        lista[b].push_back(a);
    }
}
void dfs(int node){
    ap[node]=1;
    for(auto x:lista[node]){
        if(!ap[x]){
            dfs(x);
            if(dp[x]>0)
                dp[node]+=dp[x];
        }
    }
}
void solve(){
    dfs(1);
    int ma=INT_MIN;
    for(int i=1; i<=n; i++){
        ma=max(ma,dp[i]);
    }
    out << ma;
}
int main(){
    read();
    solve();
    return 0;
}