Cod sursa(job #2700610)

Utilizator robeert.77Chirica Robert robeert.77 Data 28 ianuarie 2021 11:33:46
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 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];
bool reached[N];
vector<int> tree[N];

void findBestValues(int node) {
    reached[node] = true;
    for (int it : tree[node])
        if (!reached[it]) {
            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[a].push_back(b);
        tree[b].push_back(a);
    }

    findBestValues(1);
    int maxValue = INT_MIN;
    for (int i = 1; i <= n; i++)
        maxValue = max(maxValue, bestValue[i]);
    fout << maxValue;
    return 0;
}
/*
7
-1 3 2 -5 -2 4 -1
1 2
3 1
7 1
5 6
5 3
4 3


17
7 2 -2 -2 3 4 -1 -8 3 1 -3 7 2 5 -9 1 10
7 2
8 2
9 2
4 6
4 2
1 4
3 12
12 10
12 5
5 1
14 15
16 17
13 17
15 11
17 11
11 1
*/