Cod sursa(job #3316538)

Utilizator MihneaStoicaMihnea Teodor Stoica MihneaStoica Data 19 octombrie 2025 09:25:15
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

/**
 * Problem: Asmax
 * URL: https://infoarena.ro/problema/asmax
 * TL: 25 ms
 * ML: 64 MB
 */
using ll = long long;
// #define int ll

vector<int> a;
vector<vector<int>> adj;

int maxx = INT32_MIN;

int dfs(int x, int tt = 0) {
    int sum = a[x];
    for (auto y : adj[x]) {
        if (y != tt) {
            int val = dfs(y, x);
            if (val > 0) {
                sum += val;
            }
        }
    }
    maxx = max(maxx, sum);
    return sum;
}

void tc() {
    int n; cin >> n;

    a.resize(n + 1);
    adj.resize(n + 1);
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i ++) {
        int x, y; cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    dfs(1);

    cout << maxx << '\n';
}

#define MULTITEST 0
#define FILEIO 1
#define FILE "asmax"

signed main() {
#if FILEIO && !defined(LOCAL)
    (void)!freopen(FILE ".in", "r", stdin); (void)!freopen(FILE ".out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#if MULTITEST
    int T; cin >> T; while (T --) tc();
#else
    tc();
#endif
    return 0;
}