Pagini recente » Cod sursa (job #2883859) | Cod sursa (job #396573) | Cod sursa (job #3000281) | Cod sursa (job #240289) | Cod sursa (job #2535015)
#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);5
-1 1 3 1 -1
4 1
1 3
1 2
4 5
}
}
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();
}