Cod sursa(job #2782514)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 12 octombrie 2021 16:09:48
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << '\n'
#define debugsp(x) cerr << #x << " " << x << ' '

using namespace std;

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

const int INF = 2e9;
const int N = 16e3;

int v[1 + N], dp[2][1 + N];
vector <int> G[1 + N];

void DFS (int node, int parent = 0) {
    dp[0][node] = -INF;
    dp[1][node] = v[node];

    for (int to : G[node]) {
        if (to != parent) {
            DFS(to, node);
            dp[0][node] = max(dp[0][node], max(dp[0][to], dp[1][to]));

            if (dp[1][to] > 0)
                dp[1][node] += dp[1][to];
        }
    }
}

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

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

    DFS(1);

    out << max(dp[0][1], dp[1][1]) << '\n';
    return 0;
}