Cod sursa(job #3351900)

Utilizator rapidu36Victor Manz rapidu36 Data 22 aprilie 2026 10:23:23
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <vector>

using namespace std;

const int INF = 1e9;

void dfs(int x, vector <vector <int>> &a, vector <bool> &viz, vector <int> &val) {
    viz[x] = true;
    for (auto y: a[x]) {
        if (!viz[y]) {
            dfs(y, a, viz, val);
            if (val[y] > 0) {
                val[x] += val[y];
            }
        }
    }
}

int main() {
    ifstream in("asmax.in");
    ofstream out("asmax.out");
    int n;
    in >> n;
    vector <int> val(n + 1, -INF);
    for (int i = 1; i <= n; i++) {
        in >> val[i];
    }
    vector <vector <int>> a(n + 1);
    vector <bool> viz(n + 1, false);
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1, a, viz, val);
    in.close();
    for (int i = 1; i <= n; i++) {
        if (!viz[i]) {
            dfs(i, a, viz, val);
        }
    }
    int val_max = -INF;
    for (auto x: val) {
        val_max = max(val_max, x);
    }
    out << val_max << "\n";
    out.close();
    return 0;
}