Cod sursa(job #1849577)

Utilizator yosemiteYosemite yosemite Data 17 ianuarie 2017 18:09:36
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
const int nMax = 16003;
const int inf = 1e9;
vector <int> Graf[nMax];
int cost[nMax], dp[nMax];
bool viz[nMax];
inline void Dfs(int nod) {
    viz[nod] = 1;
    dp[nod] = cost[nod];
    for(auto i : Graf[nod]) {
        if(!viz[i]) {
            Dfs(i);
            dp[nod] = max(dp[nod], dp[nod] + dp[i]);
        }
    }
}
int main()
{
    int n , x , y, ans = -inf;
    f >> n;
    for(int i = 1; i <= n; i++) {
        f >> cost[i];
    }
    for(int i = 1; i < n; i++) {
        f >> x >> y;
        Graf[x].push_back(y);
        Graf[y].push_back(x);
    }
    Dfs(1);
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
    }
    g << ans << "\n";
    return 0;
}