Cod sursa(job #1803770)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 noiembrie 2016 19:56:25
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')

typedef long long int lld;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int NMAX = 16e3;

int N;
int val[NMAX + 5];
vector<int> V[NMAX + 5];
bitset < NMAX + 5 > viz;
int dp[NMAX + 5];

void dfs(int x) {
  viz[x] = 1;

  dp[x] = val[x];

  for (int y : V[x]) {
    if (!viz[y]) {
      dfs(y);
      dp[x] += max(0, dp[y]);
    }
  }
}

int main() {
  cin.sync_with_stdio(false);

  freopen("asmax.in", "r", stdin);
  freopen("asmax.out", "w", stdout);

  cin >> N;

  for (int i = 1; i <= N; ++i) {
    cin >> val[i];
  }

  for (int i = 1, x, y; i <= N - 1; ++i) {
    cin >> x >> y;
    V[x].push_back(y);
    V[y].push_back(x);
  }

  dfs(1);

  cout << *max_element(dp + 1, dp + N + 1);

  return 0;
}