Cod sursa(job #3232559)

Utilizator elbarosPetre Tudor elbaros Data 30 mai 2024 23:16:34
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <vector>
#include <algorithm>
#include <fstream>
#include <limits>
using namespace std;

const int nmax = 16010;

int n, v[nmax], mx[nmax];
vector<int> g[nmax];
bool used[nmax];

void dfs(int node) {
    if (used[node]) {
        return;
    }
    used[node] = 1;

    int sum = 0;
    for (int vec : g[node]) {
        if (!used[vec]) {
            dfs(vec);
            sum += max(mx[vec], 0);
        }
    }
    mx[node] = max(v[node], v[node] + sum);
}
            
int main() {
    ifstream in("asmax.in");
    ofstream out("asmax.out");

    in >> n;
    for (int i = 1; i <= n; i++) {
        in >> v[i];
    }

    for (int i = 1; i < n; i++) {
        int a, b;
        in >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    dfs(1);

    int res = numeric_limits<int>::min();
    for (int i = 1; i <= n; i++) {
        res = max(res, mx[i]);
    }

    out << res << std::endl;
    return 1;
}