Cod sursa(job #1426557)

Utilizator StefanRARapeanu-Andreescu Stefan StefanRA Data 29 aprilie 2015 22:02:15
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>
#include <vector>
#include <bitset>
int n, i, x, y, v[16001], maxsum=-1<<31;
std::vector<int> graph[16001];
std::bitset<16001> visited;
void dfs(int vertex)
{
	visited[vertex]=true;
	for (int j=0;j<graph[vertex].size();j++)
		if (!visited[graph[vertex][j]])
		{
			dfs(graph[vertex][j]);
			if (v[graph[vertex][j]]>0)
				v[vertex]+=v[graph[vertex][j]];
		}
}
int main()
{
	freopen("asmax.in", "r", stdin);
	freopen("asmax.out", "w", stdout);
	scanf("%d", &n);
	for (i=1;i<=n;i++)
		scanf("%d", &v[i]);
	for (i=1;i<n;i++)
	{
		scanf("%d %d", &x, &y);
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	dfs(1);
	for (i=1;i<=n;i++)
		if (v[i]>maxsum)
			maxsum=v[i];
	printf("%d", maxsum);
	return 0;
}