Cod sursa(job #2700354)

Utilizator robeert.77Chirica Robert robeert.77 Data 27 ianuarie 2021 14:47:25
Problema Asmax Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin ("asmax.in");
ofstream fout ("asmax.out");
const int N = 16e3 + 5;
int n, nodeValue[N], bestValue[N];
vector<int> tree[N];

void findBestValues(int node) {
    int positiveSum = 0;
    for (auto it : tree[node]) {
    // for (vector<int>::iterator it = treee[node].begin(); it != treee[node].end(); it++) {
        findBestValues(it);
        if (bestValue[it] > 0)
            positiveSum += bestValue[it];
    }
    bestValue[node] = nodeValue[node] + positiveSum;
}

int main() {
    fin.tie(0);
    fout.tie(0);
    ios::sync_with_stdio(0);
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> nodeValue[i];
    int a, b;
    for (int i = 1; i < n; i++) {
        fin >> a >> b;
        tree[min(a, b)].push_back(max(a, b));
    }

    findBestValues(1);
    int maxValue = INT_MIN;
    for (int i = 1; i <= n; i++)
        maxValue = max(maxValue, bestValue[i]);
    fout << maxValue;
    return 0;
}