Pagini recente » Cod sursa (job #708536) | Cod sursa (job #524485) | Cod sursa (job #1238280) | Cod sursa (job #2146717) | Cod sursa (job #2828499)
#include <iostream>
#include <bits/stdc++.h>
#define inf 1e9
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
int n;
int valori[16001];
vector<int> la[16001];
int viz[16001];
int get_max(int from)
{
viz[from] = 1;
int mx = valori[from];
for (auto& to: la[from])
{
if (viz[to])
continue;
mx += get_max(to);
}
viz[from] = 0;
return max(mx, 0);
}
int main()
{
in >> n;
for (int i = 1; i <= n; i++)
in >> valori[i];
for (int i = 1; i < n; i++)
{
int from, to;
in >> from >> to;
la[from].push_back(to);
la[to].push_back(from);
}
int res = -inf;
for (int i = 1; i <= n; i++)
{
res = max(res, get_max(i));
}
out << res;
return 0;
}