Pagini recente » Cod sursa (job #44256) | Cod sursa (job #775519) | Cod sursa (job #412362) | Cod sursa (job #1019120) | Cod sursa (job #2553803)
#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 k;
int dfs(int curent){
viz[curent]=1;
for(k=1;k<=n;k++){
if(a[curent][k]==1&&viz[k]==0){
cost+=val[k];
costmax=max(costmax,cost);
//cout<<k<<" "<<cost<<" "<<costmax<<endl;
dfs(k);
//cost-=val[k];
}
}
}
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;
}
*/
cost+=val[1];
dfs(1);
costmax=max(costmax,cost);
fout<<costmax;
return 0;
}