Pagini recente » Cod sursa (job #2582154) | Cod sursa (job #208258) | Cod sursa (job #3142348) | Cod sursa (job #1320782) | Cod sursa (job #3280998)
#include <bits/stdc++.h>
#define oo 2000000001
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
/**
2 4 6
1 3
2 10
4 6
5 8
3
*/
int n;
int val[16005];
int root[16005];
int dp[16005];
vector<int> G[16005];
void DFS(int x)
{
if(G[x].empty())
dp[x] = val[x];
for(auto e : G[x])
DFS(e);
for(auto e : G[x])
{
dp[x] = max(dp[x], dp[e] + val[x]);
}
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i,j,x,y;
fin >> n;
for(i = 1; i <= n; i++)
fin >> val[i];
for(i = 1; i <= n; i++)
root[i] = 1;
for(i = 1; i < n; i++)
{
fin >> x >> y;
G[x].push_back(y);
root[y] = 0;
}
for(i = 1; i <= n && root[i] == 0; i++)
;
int rad = i;
DFS(rad);
fout << dp[rad] + val[rad];
return 0;
}