Cod sursa(job #3214420)

Utilizator DKMKDMatei Filibiu DKMKD Data 14 martie 2024 09:20:51
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;

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

int n;
vector<int> val(vec_len);
vector<vector<int>> graph(vec_len);

const int vec_len = 200001;

int res = INT_MIN;
bitset<vec_len> vis;
void dfs(int node){
  vis[node] = true;
  for (int next : graph[node])
    if (!vis[next]){
      dfs(next);
      if (val[next] > 0)
        val[node] += val[next];
    }
  res = max(res, val[node]);
}

int main(){
  fin >> n;
  for (int i = 1; i <= n; i++)
    fin >> val[i];
  int x, y;
  for (int i = 1; i < n; i++){
    fin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(1);
  fout << res;
  return 0;
}