Cod sursa(job #1132927)

Utilizator toranagahVlad Badelita toranagah Data 4 martie 2014 08:55:09
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
//Problem asmax from Infoarena
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

const int INF = 1 << 30;
const int MAX_N = 16100;

int N;
int d[MAX_N];
int best[MAX_N];
int ans;
vector<int> graph[MAX_N];

void dfs(int node);

int main() {
  fin >> N;
  for (int i = 1; i <= N; i += 1) {
    fin >> d[i];
    best[i] = -INF;
  }

  for (int i = 1, x, y; i < N; i += 1) {
    fin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  ans = -INF;
  dfs(1);

  fout << ans;
  return 0;
}

void dfs(int node) {
  best[node] = d[node];

  for (auto x: graph[node]) {
    if (best[x] == -INF) {
      dfs(x);
      if (best[x] > 0) {
        best[node] += best[x];
      }
    }
  }
  ans = max(ans, best[node]);
}