Pagini recente » Cod sursa (job #1881940) | Cod sursa (job #877660) | Cod sursa (job #2336676) | Cod sursa (job #540549) | Cod sursa (job #2140648)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define nmax 16005
ifstream f("asmax.in");
ofstream g("asmax.out");
int cost[nmax],dp[nmax],n;
bool viz[nmax];
vector<int>Q[nmax];
void dfs(int nod)
{
dp[nod]=cost[nod];
viz[nod]=true;
for (auto w:Q[nod])
{
if (!viz[w])
{
dfs(w);
if (dp[w]>0)
dp[nod]+=dp[w];
}
}
}
void read()
{
f>>n;
for (int i=1;i<=n;++i)
f>>cost[i];
for(int i=1;i<n;++i)
{
int a,b;
f>>a>>b;
Q[a].push_back(b);
Q[b].push_back(a);
}
}
int main()
{
read();
dfs(1);
int mx=-1;
for (int i=1;i<=n;++i)
mx=max(dp[i],mx);
g<<mx;
return 0;
}