Cod sursa(job #3148425)

Utilizator andrei1807Andrei andrei1807 Data 1 septembrie 2023 10:37:40
Problema Asmax Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

const int NMAX = 16003;

vector<int> g[NMAX];
int val[NMAX], s[NMAX];
bool vis[NMAX];

void dfs(int nod) {
    vis[nod] = true;
    s[nod] += val[nod];

    for (auto v : g[nod]) {
        if (!vis[v]) {
            dfs(v);
            if (s[v] > 0)
                s[nod] += s[v];
        }
    }
}

int main() {

    int n;
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> val[i];

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

    dfs(1);
    fout << s[1];

    return 0;
}