Cod sursa(job #3175292)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 25 noiembrie 2023 16:40:13
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int maxN = 16005;
int v[maxN], sp[maxN], ans = -0x3f3f3f3f;
vector <int> G[maxN];

void dfs(int nod, int tata) {
    sp[nod] += v[nod];
    for (int vecin : G[nod]) {
        if (vecin == tata) {
            continue;
        }
        dfs(vecin, nod);
        if (sp[vecin] > 0) {
            sp[nod] += sp[vecin];
        }
    }
    ans = max(ans, sp[nod]);
}

int main() {
    int n;
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> v[i];
    }
    for (int i = 1; i < n; i++) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 0);
    fout << ans;
    return 0;
}