Pagini recente » Cod sursa (job #169432) | Cod sursa (job #1773498) | Istoria paginii utilizator/bibere_crn | Cod sursa (job #1781498) | Cod sursa (job #1704964)
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int n,cost[16001];
vector <int> a[16001];
bool viz[16001];
void dfs(int x)
{
int i,y;
viz[x]=true;
for(i=0; i<a[x].size(); i++)
{
y=a[x][i];
if(!viz[y])
{
dfs(y);
if(cost[y]>0)
cost[x]+=cost[y];
}
}
}
int main()
{
freopen("asmax.in","r",stdin);
freopen("asmax.out","w",stdout);
int i,x,y,maxi=0;
bool ok=false;
cin>>n;
for(i=1; i<=n; i++)
cin>>cost[i];
while(cin>>x>>y)
{
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
for(i=1; i<=n; i++)
if(maxi<cost[i])
maxi=cost[i];
cout<<maxi;
return 0;
}
/*int dfs(int h) Varianta mea (nu prea merge)
{
int i,s=0,x;
bool ok=true;
for(i=1; i<=n; i++)
{
if(a[h][i]==1 && v[i]==0)
{
v[i]=1;
x=dfs(i);
if(x>0)
s+=x;
ok=false;
}
if(ok==true)
if(cost[h]>0)
return cost[h];
else
return 0;
}
if(s+cost[h]>0)
{
cout<<"Am adaugat: "<<s+cost[h]<<'\n';
return s+cost[h];
}
return 0;
}*/