Cod sursa(job #578536)

Utilizator liv182copoiu liviu liv182 Data 11 aprilie 2011 12:55:08
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <stdio.h>
#include <vector>
#define N 20000
using namespace std;
vector<int> v[N];
int n,val[N],costt[N],sol=-10000000;
bool folos[N];

/*void DFS(int nod)
{
	viz[nod] = 1;
	for(vector<int> :: iterator it = Arb[nod].begin() ; it != Arb[nod].end() ; it++)
		if(!viz[*it])
		{
			DFS(*it);
			if(V[*it] > 0)
				V[nod] += V[*it];
		}*/

void dfs(int varf)
{
	int i, cost = 0;
	folos[varf] = 1;
	for(i=0;i<v[varf].size();++i)
	{
		if(!folos[v[varf][i]])
		{
			dfs(v[varf][i]);
			if(costt[v[varf][i]]>0)
				cost+=costt[v[varf][i]];
		}
	}
	costt[varf] = val[varf]+cost;
	if(sol < costt[varf])
		sol = costt[varf];
}
int main()
{
	freopen("asmax.in","r",stdin);
	freopen("asmax.out","w",stdout);
	int i,x,y;
	scanf("%d", &n);
	for(i=1;i<=n;++i)
		scanf("%d",&val[i]);

	for(i=1;i<n;++i)
	{
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1);
	printf("%d",sol);
}