Pagini recente » Cod sursa (job #2854682) | Cod sursa (job #1364648) | Cod sursa (job #1462738) | Cod sursa (job #221199) | Cod sursa (job #1342272)
#include <cstdio>
#include <vector>
#include <algorithm>
#define pb push_back
#define nmax 16016
using namespace std;
vector <int> a[nmax];
int n, i, max_tree=0, x, y;
bool used[nmax];
int value[nmax];
void dfs(int node)
{
used[node]=true;
vector<int>::iterator it;
for(it=a[node].begin(); it!=a[node].end(); ++it)
if(!used[*it])
{
dfs(*it);
value[node]=max(value[node], value[node]+value[*it]);
}
max_tree=max(max_tree, value[node]);
}
int main()
{
freopen("asmax.in", "rt", stdin);
freopen("asmax.out", "wt", stdout);
scanf("%d", &n);
for(i=1; i<=n; ++i)
scanf("%d", &value[i]);
for(i=1; i<n; ++i)
{
scanf("%d%d", &x, &y);
a[x].pb(y);
a[y].pb(x);
}
dfs(1);
printf("%d\n", max_tree);
return 0;
}