Cod sursa(job #3198534)

Utilizator Info_MasterAugustin-Ionut Info_Master Data 29 ianuarie 2024 18:52:53
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
int n;
const int nmax = 16005;
int maxdp[nmax], val[nmax];
vector<int> a[nmax];

void dfs(int node, int tactu_mare){
  maxdp[node] = val[node];
  for(int vec : a[node]){
    if(vec != tactu_mare){
      dfs(vec, node);
      if(maxdp[vec] > 0){
        maxdp[node] += maxdp[vec];
      }
    }
  }
}
int main(){
  f >> n;
  for(int i = 1; i <= n; i++){
    f >> val[i];
  }
  for(int i = 1; i < n; i++){
    int x, y;
    f >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  dfs(1, 0);
  int sol = INT_MIN;
  for(int i = 1; i <= n; i++){
    sol = max(sol, maxdp[i]);
  }
  g << sol << '\n';
}