Pagini recente » Cod sursa (job #870170) | Cod sursa (job #2449030) | Cod sursa (job #2158477) | Cod sursa (job #240230) | Cod sursa (job #2535019)
#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)
{
int x;
viz[nod] = 1;
for(const auto it : g[nod])
{
if(viz[it] == 0)
{
x = it;
viz[it] = 1;
dp[nod] = max(dp[it-1] + cost[it], dp[it]);
DFS(it);
}
}
Max = max(dp[nod], Max);
}
void solve()
{
DFS(1);
cout << Max;
}
int main()
{
read();
solve();
}