Cod sursa(job #2908164)

Utilizator NanuGrancea Alexandru Nanu Data 1 iunie 2022 20:39:46
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>
#include <vector>
using namespace std;

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

#define DIM 16000

int n;
bool sel[DIM + 1];
int cost[DIM + 1];
vector <int> G[DIM + 1];

static inline void dfs(int nod) {
  sel[nod] = 1;
  for(auto e : G[nod]) {
    if(!sel[e]) {
      dfs(e);
      cost[nod] = max(cost[nod], cost[e] + cost[nod]);
    }
  }
}

int main() {
  fin >> n;
  for(int i = 1; i <= n; i++)
    fin >> cost[i];
  
  for(int i = 1; i < n; i++) {
    int x, y;

    fin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  dfs(1);

  int maxim = cost[1];
  for(int i = 2; i <= n; i++)
    maxim = max(maxim, cost[i]);
  fout << maxim;

  return 0;
}