Pagini recente » Cod sursa (job #2069230) | Cod sursa (job #522575) | Cod sursa (job #1366907) | Cod sursa (job #1016067) | Cod sursa (job #1670397)
#include <iostream>
#include <cstdio>
using namespace std;
int n,cost[16001],grad[16001];
bool a[16001][16001];
bool verif()
{
for(int i=1; i<=n; i++)
if(grad[i]>1)
return true;
return false;
}
int main()
{
freopen("asmax.in","r",stdin);
freopen("asmax.out","w",stdout);
cin>>n;
int i,x,y,j;
for(int i=1; i<=n; i++)
cin>>cost[i];
while(cin>>x>>y)
{
a[x][y]=a[y][x]=true;
grad[x]++;
grad[y]++;
}
while(verif()==true)
{
for(i=1; i<=n; i++)
if(grad[i]==1)
{
if(cost[i]>0)
for(j=1; j<=n; j++)
if(a[i][j]==true)
{
cost[j]+=cost[i];
j=n;
}
}
for(i=1; i<=n; i++)
grad[i]--;
}
int maxi=0;
for(i=1; i<=n; i++)
if(grad[i]>=0 and cost[i]>0)
maxi+=cost[i];
cout<<maxi;
return 0;
}
/* varianta mai mult mai buna:
void dfs (int x)
{
s[x]=v[x];
p=lst[x];
while(p!=0)
{
y=vf[p];
if(!viz[y])
{
dfs(y);
if(s[y]>0)
s[x]+=s[y];
}
p=urm[p];
}
*/