Cod sursa(job #2195301)

Utilizator trifangrobertRobert Trifan trifangrobert Data 15 aprilie 2018 22:25:42
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <algorithm>
#include <bitset>
#include <fstream>
#include <vector>

using namespace std;

const int DIM = 16010;
long long n, v[DIM], dp[DIM];
vector <int> graph[DIM];
bitset <DIM> viz;

void DFS(int node)
{
	viz[node] = 1;
	for (auto i : graph[node])
		if (viz[i] == 0)
			DFS(i);
	long long s = 0;
	for (auto i : graph[node])
		if (dp[i] > 0)
			s += dp[i];
	dp[node] = s + v[node];
}

int main()
{
	ifstream fin("asmax.in");
	ofstream fout("asmax.out");
	fin >> n;
	for (int i = 1;i <= n;++i)
		fin >> v[i];
	int x, y;
	for (int i = 1;i < n;++i)
	{
		fin >> x >> y;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	DFS(1);
	long long Max = -1;
	for (int i = 1;i <= n;++i)
		Max = max(Max, dp[i]);
	fout << Max << "\n";
	fin.close();
	fout.close();
	return 0;
}