Pagini recente » Cod sursa (job #2577743) | Cod sursa (job #163480) | Cod sursa (job #858143) | Cod sursa (job #2300056) | Cod sursa (job #578566)
Cod sursa(job #578566)
#include <stdio.h>
#include <vector>
using namespace std;
const int N=17000;
int val[N],cost[N],smax=-1000000,n;
vector<int> a[N];
bool b[N];
void citire()
{
int x,y,i;
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&val[i]);
for (i=1;i<=n-1;i++)
{
scanf("%d%d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
if (b[x])
return;
b[x]=true;
cost[x]=val[x];
for (int i=0;i<a[x].size();i++)
{
int y=a[x][i];
if (!b[y])
{
dfs(y);
if (cost[y]>0)
cost[x]+=cost[y];
}
}
if (cost[x]>smax)
smax=cost[x];
}
void afisare()
{
printf("%d",smax);
}
int main()
{
freopen("asmax.in","r",stdin);
freopen("asmax.out","w",stdout);
citire();
dfs(1);
afisare();
return 0;
}