Pagini recente » Cod sursa (job #668430) | Cod sursa (job #3289159) | Cod sursa (job #1758266) | Cod sursa (job #2907122) | Cod sursa (job #2828501)
#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 max_in_node[16001];
int get_max(int from)
{
if (max_in_node[from] != -inf)
return max_in_node[from];
viz[from] = 1;
int mx = valori[from];
for (auto& to: la[from])
{
if (viz[to])
continue;
mx += get_max(to);
}
viz[from] = 0;
int ret = max(mx, 0);
max_in_node[from] = ret;
return ret;
}
int main()
{
in >> n;
for (int i = 1; i <= n; i++)
{
in >> valori[i];
max_in_node[i] = -inf;
}
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;
}