Pagini recente » Cod sursa (job #982491) | Cod sursa (job #2857993) | Cod sursa (job #3180879) | Cod sursa (job #1885771) | Cod sursa (job #2909982)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define cin fin
#define cout fout
#define N 16005
int f[N], c[N], v[N], n, x, y, cost, pct, rez;
vector < vector < int > > g;
int dfs(int nod)
{
f[nod] = 1;
c[nod] = v[nod];
for(auto t : g[nod])
{
if(f[t] == 0)
{
int rez = dfs(t);
c[nod] = max(c[nod]+rez,c[nod]);
}
}
return c[nod];
}
int main()
{
cin >> n;
g.resize(n+5);
pct = 1, cost = -1005;
for(int i = 1 ; i <= n ; i++)
{
cin >> v[i];
if(v[i] > cost)
{
cost = v[i];
pct = i;
}
}
for(int i = 1 ; i < n ; i++)
{
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(pct);
for(int i = 1 ; i <= n ; i++)
{
rez = max(rez,c[i]);
}
cout << rez;
return 0;
}