Cod sursa(job #555687)

Utilizator cristiprgPrigoana Cristian cristiprg Data 15 martie 2011 18:14:10
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <cstdio>
#include <vector>
#include <climits>
using namespace std;
#define DIM 16005
#define pb push_back
#define IT vector<int>::iterator

int smax[DIM], n, v[DIM];
vector<int>G[DIM];

void read()
{
	FILE *f = fopen("asmax.in", "r");
	fscanf(f, "%d", &n);
	for (int i = 1; i <= n; ++i)
		fscanf(f, "%d", &smax[i]);
		
	for (int i = 1, x, y; i < n; ++i)
		fscanf(f, "%d%d", &x, &y), G[x].pb(y), G[y].pb(x);
	fclose(f);
}

void DFS(int i)
{
	v[i] = 1;
	for (IT it = G[i].begin(); it != G[i].end(); ++it)
		if (!v[*it])
		{
			DFS(*it);
			if (smax[*it] > 0)
				smax[i] += smax[*it];
		}
}

int main()
{
	read();
	DFS(1);
	int max = INT_MIN;
	for (int i = 1; i <= n; ++i)
		if (max < smax[i])
			max = smax[i];
	FILE *f = fopen("asmax.out", "w");
	fprintf(f, "%d\n", max);
	fclose(f);
	return 0;
}