Pagini recente » Cod sursa (job #1276839) | Cod sursa (job #2969899) | Cod sursa (job #388689) | Cod sursa (job #898763) | Cod sursa (job #1860561)
#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;
dp[nod] = max(dp[nod], dp[nod] + dp[it]);
dfs(it, nod);
}
}
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=-((1<<31)-1);
for(int i = 1; i <= n; ++i){
maxx=max(dp[i], maxx);
}
fout<<maxx;
return 0;
}