Cod sursa(job #2700599)

Utilizator robeert.77Chirica Robert robeert.77 Data 28 ianuarie 2021 11:18:39
Problema Asmax Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 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, bestValue[N];
vector<int> tree[N];

void findBestValues(int node) {
    for (int it : tree[node]) {
        findBestValues(it);
        if (bestValue[it] > 0)
            bestValue[node] += bestValue[it];
    }
}

int main() {
    fin.tie(0);
    fout.tie(0);
    ios::sync_with_stdio(0);
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> bestValue[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;
}