Pagini recente » Cod sursa (job #2528755) | Cod sursa (job #2828518)
#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 maxi = -inf;
int get_max(int from)
{
viz[from] = 1;
int mx = valori[from];
for (auto& to: la[from])
{
if (viz[to])
continue;
mx += max(0, get_max(to));
}
maxi = max(maxi, mx);
return mx;
}
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);
}
get_max(1);
out << maxi;
return 0;
}