Cod sursa(job #2051776)

Utilizator LittleWhoFeraru Mihail LittleWho Data 29 octombrie 2017 15:33:48
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>

#define N_MAX 16001
#define INF 0xfffffff

using namespace std;

int n;
vector<int> nodes[N_MAX];
int w[N_MAX];
int S[N_MAX];
int sol = -INF;

void dfs(int node, int pre)
{
    S[node] = w[node];
    for (auto it = nodes[node].begin(); it != nodes[node].end(); it++) {
        if (*it != pre) {
            dfs(*it, node);
            if (S[*it] > 0) S[node] += S[*it];
        }
    }
    sol = max(sol, S[node]);
}

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

    cin >> n;
    //cout << n << endl;

    for (int i = 0; i < n; i++) {
        cin >> w[i];
        //cout << w[i] << ' ';
        S[i] = -INF;
    }
    //cout << endl;

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

    dfs(0, 0);
    cout << sol << '\n';

    return 0;
}