Cod sursa(job #1563207)

Utilizator valentin50517Vozian Valentin valentin50517 Data 5 ianuarie 2016 19:08:38
Problema Asmax Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
typedef struct nod{
	int key,val;
	nod* next;
}* lnod;
lnod A[16010];
bool B[16010];
int maxi,N,V[16010],sef[16010],boss; 
void add(lnod &a,int key){
		lnod b = new nod;
		b->key = key;
		b->next = a;
		a = b;
}

int rez(int key){
	int rs = 0;
	if(A[key]){
		for(lnod l = A[key];l;l = l->next){		
			rs+=max(0,rez(l->key));
		}
	}
	rs+=V[key];
	if(maxi < rs) maxi = rs;
	return rs;
}
int main(){
	fin >> N;
	for(int i = 1;i<=N;i++) fin >> V[i];
	for(int i = 1;i<N;i++){
			int x,y;
			fin >> x >> y;
			if(sef[x] == 0) sef[x] = x;
			sef[y] = x;
			add(A[x],y);
	}
	boss = 1;
	while(sef[boss] != boss) boss = sef[boss];
	rez(boss);
	if(maxi < 0) maxi = 0;
	fout << maxi;
		return 0;
}