Cod sursa(job #899425)

Utilizator Detrol2kGuianu Leon Detrol2k Data 28 februarie 2013 14:21:47
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int n, cost[16001], i, j, x, y, max_cost = -1<<30;
vector<int> l[16001];
bool viz[16001];

void dfs(int x)
{
	viz[x] = true;
	for(int i=0; i<l[x].size(); i++)
		if(viz[l[x][i]] == false)
		{
			dfs(l[x][i]);
			if(cost[l[x][i]] > 0)
				cost[x] += cost[l[x][i]];
		}
	if(cost[x] > max_cost) max_cost = cost[x];
}

int main()
{
	ifstream fin("asmax.in");
	ofstream fout("asmax.out");
	
	//Read
	fin>>n;
	for(i=1; i<=n; i++)
		fin>>cost[i];
	for(i=1; i<n; i++)
	{
		fin>>x>>y;
		l[x].push_back(y);
		l[y].push_back(x);
	}


	//Compute	
	dfs(1);


	//Print
	fout<<max_cost;
	for(i=1; i<=n; i++)
		cout<<cost[i]<<" ";
	
}