Pagini recente » Cod sursa (job #1905455) | Cod sursa (job #3248219) | Cod sursa (job #1454318) | Cod sursa (job #2931012) | Cod sursa (job #2535030)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define cin fin
#define cout fout
#define Nmax 16001
int n;
int cost[Nmax];
int dp[Nmax];
int Max = -INT_MAX;
vector < int > g[Nmax];
bitset < Nmax > viz;
void read()
{
int x, y;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> cost[i];
}
for(int i=1; i<=n-1; i++)
{
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
}
void DFS(int nod)
{
viz[nod] = 1;
dp[nod] = cost[nod];
for(const auto it : g[nod])
{
if(viz[it] == 0)
{
DFS(it);
dp[nod] = max(dp[nod] + dp[it], dp[nod]);
}
Max = max(dp[nod], Max);
}
}
void solve()
{
DFS(1);
cout << Max;
}
int main()
{
read();
solve();
}