Cod sursa(job #3336981)

Utilizator marelucaMare Luca Ghita mareluca Data 26 ianuarie 2026 20:08:28
Problema Asmax Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

const int NMAX = 16005;

// Suma maxima daca incepem
// subgraful din nodul [i]
std::vector<int> dp;
std::vector<int> graph[NMAX];

void DFS(int node, int parent) {
    for (int neighbour : graph[node]) {
        if (neighbour != parent) {
            DFS(neighbour, node);

            if (dp[neighbour] > 0) {
                dp[node] += dp[neighbour];
            }
        }
    }
}

int main() {
    std::ifstream fin("asmax.in");
    std::ofstream fout("asmax.out");

    int n;
    fin >> n;
    dp.resize(n + 1, 0);

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

    for (int i = 1; i <= n; ++i) {
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    DFS(1, 0);

    fout << *std::max_element(dp.begin() + 1, dp.end()) << '\n';
    return 0;
}