Pagini recente » Cod sursa (job #3200917) | Cod sursa (job #3229359)
#include <fstream>
#include <climits>
#include <vector>
using namespace std;
const int N = 16e3;
vector <int> a[N + 1];
int val[N + 1];
bool viz[N + 1];
int n;
int dfs(int x, int &smax)
{
viz[x] = true;
int s = val[x];
for (int y : a[x])
if (!viz[y])
s += max(0, dfs(y, smax));
smax = max(smax, s);
return s;
}
int main()
{
ifstream in("asmax.in");
ofstream out("asmax.out");
in >> n;
for (int i = 1; i <= n; ++i)
in >> val[i];
for (int i = 1, x, y; i < n && in >> x >> y; ++i)
a[x].push_back(y), a[y].push_back(x);
int smax = INT_MIN;
dfs(1, smax);
out << smax << endl;
}