Pagini recente » Cod sursa (job #2191384) | Cod sursa (job #862221) | Cod sursa (job #1701741) | Cod sursa (job #1278607) | Cod sursa (job #1860566)
#include <iostream>
#include <fstream>
#include <vector>
#define MAX 16005
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, dp[MAX];
vector <int> G[MAX];
void dfs(int nod, int dad){
for(auto it: G[nod]){
if(it == dad)
continue;
dfs(it, nod);
dp[nod] = max(dp[nod], dp[nod] + dp[it]);
}
}
int main()
{
fin >> n;
for(int i = 1; i <= n; ++i)
fin >> dp[i];
for(int i = 1; i !=n; ++i){
int x, y;
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 0);
int maxx = dp[1];
for(int i = 2; i <= n; ++i){
maxx=max(dp[i], maxx);
}
fout<<maxx;
return 0;
}