Pagini recente » Cod sursa (job #2879548) | Cod sursa (job #2102771) | Cod sursa (job #2847775) | Cod sursa (job #1318328) | Cod sursa (job #1899398)
#include<bits/stdc++.h>
using namespace std;
int n, cost[16100], x, y, viz[16000],rs;
vector<int> a[16100];
int dfs(int x)
{
viz[x] = 1;
int rez=0;
for (vector<int>::iterator it = a[x].begin(); it != a[x].end(); it++)
{
if (!viz[*it])
{
rez += dfs(*it);
}
}
rs = max(rs,max(rez + cost[x], max(0,cost[x])));
return max(rez + cost[x], max(0,cost[x]));
}
int main()
{
ifstream cin("asmax.in");
ofstream cout ("asmax.out");
cin >> n;
int x = -1<<30;
for (int i = 1; i <= n ; i++) {cin >> cost[i]; x = max(x,cost[i]);}
for (int i = 1; i <= n-1; i++)
{
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
cout << max(x,rs);
}