Cod sursa(job #2928982)

Utilizator raresgherasaRares Gherasa raresgherasa Data 24 octombrie 2022 12:45:13
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NM = 16005;

vector<int>g[NM];
int mx[NM], n, used[NM];

void dfs (int nod){
  used[nod] = true;
  for (int u : g[nod]){
    if (!used[u]){
      dfs(u);
      mx[nod] = max(mx[nod], mx[nod] + mx[u]);
    }
  }
}

int main(){
  fin >> n;
  for (int i = 1; i <= n; i++){
    int x; fin >> x;
    mx[i] = x;
  }
  for (int i = 1; i < n; i++){
    int x, y; fin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(n);
  fout << *max_element(mx + 1, mx + n + 1);
}