Pagini recente » Cod sursa (job #2282963) | Cod sursa (job #27446) | Cod sursa (job #857273) | Cod sursa (job #2448587) | Cod sursa (job #2700354)
#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;
}