Cod sursa(job #1563168)

Utilizator valentin50517Vozian Valentin valentin50517 Data 5 ianuarie 2016 18:42:05
Problema Asmax Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>

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){
	B[key] = 1;
	int rs = 0;
	if(A[key]){
			for(lnod l = A[key];l;l = l->next){
					if(!B[l->key]){
						int pas = V[l->key] + rez(l->key);
						if(pas > 0) rs+=pas;
					}
			}
	}
	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);
	fout << maxi;
		return 0;
}