Cod sursa(job #2178372)

Utilizator cella.florescuCella Florescu cella.florescu Data 19 martie 2018 13:37:33
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;

const int MAXN = 16e3;

vector <int> g[MAXN + 1];
int dp[MAXN + 1], ans = INT_MIN;

void dfs(int node, int father) {
  for (auto it : g[node])
    if (father ^ it) {
      dfs(it, node);
      dp[node] = max(dp[node], dp[node] + dp[it]);
    }
  ans = max(ans, dp[node]);
}

int main()
{
    int n;
    ifstream fin("asmax.in");
    fin >> n;
    for (int i = 1; i <= n; ++i)
      fin >> dp[i];
    for (int i = 1; i < n; ++i) {
      int x, y;
      fin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    fin.close();
    dfs(1, 0);
    ofstream fout("asmax.out");
    fout << ans;
    fout.close();
    return 0;
}