Cod sursa(job #3162385)

Utilizator vladburacBurac Vlad vladburac Data 29 octombrie 2023 09:04:17
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int SIGMA = 26;
const int NMAX = 16e3;
const int MOD = 1000003;


#ifndef HOME
  ifstream fin( "grigo.in" );
  ofstream fout( "grigo.out" );
  #define cin fin
  #define cout fout
#endif // HOME

int v[NMAX+1];
vector <int> edges[NMAX+1];
int best[NMAX+1];
int ans = 0;

void dfs( int node, int par ) {
  best[node] = v[node];
  for( auto vec: edges[node] ) {
    if( vec == par )
      continue;
    dfs( vec, node );
    best[node] += max( 0, best[vec] );
  }
  ans = max( ans, best[node] );
}
int main() {
  ios_base::sync_with_stdio( false );
  cin.tie( NULL );
  cout.tie( NULL );
  int n, i, a, b;
  cin >> n;
  for( i = 0; i < n; i++ )
    cin >> v[i];
  for( i = 0; i < n - 1; i++ ) {
    cin >> a >> b;
    a--, b--;
    edges[a].push_back( b );
    edges[b].push_back( a );
  }
  dfs( 0, -1 );
  cout << ans;
  return 0;
}