Pagini recente » Cod sursa (job #1474975) | Cod sursa (job #1728221) | Cod sursa (job #2361576) | Cod sursa (job #2100008) | Cod sursa (job #2553784)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, val[16005], a[16000][16000], viz[16005];
int costmax=-1e9, cost;
int dfs(int curent){
int k;
viz[curent]=1;
for(k=1;k<=n;k++){
if(a[curent][k]==1)
if(viz[k]==0){
cost+=val[k];
costmax=max(costmax,cost);
dfs(k);
}
}
//cout<<curent<<" "<<cost<<" "<<costmax<<endl;
}
int i, x, y, j;
int main()
{
fin>>n;
for(i=1;i<=n;i++){
fin>>val[i];
}
for(i=1;i<=n-1;i++){
fin>>x>>y;
a[x][y]=1;
a[y][x]=1;
}
/*
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
fout<<a[i][j]<<" ";
}
fout<<endl;
}
*/
dfs(1);
costmax=max(costmax,cost);
fout<<costmax;
return 0;
}