Cod sursa(job #2960292)

Utilizator NiffSniffCojocaru Calin Marcu NiffSniff Data 3 ianuarie 2023 23:31:07
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const int N = 16010;
string file = "asmax";
ifstream cin(file + ".in");
ofstream cout(file + ".out");
vector <int> a[N + 1], v, scor;
bitset <N + 1> viz;
int n, cnt, smax(-1e9);


void citire()
{
	int x, y;
	cin >> n;
	scor.resize(n + 1);
	v.push_back(0); // v[0] = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> x;
		v.push_back(x);
		smax = smax(smax, x);
	}
	for (int i = 1; i < n; i++)
	{
		cin >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
}

void DFS(int x)
{
	viz[x] = 1;
	scor[x] = v[x];
	for (int y : a[x])
	{
		if (!viz[y])
		{
			DFS(y);
			if (scor[y] > 0)
			{
				scor[x] += scor[y];
			}
		}
	}
}
int main()
{
	citire();
	DFS(1);
	for (int i : scor)
	{
		if (smax < i)
			smax = i;
	}
	cout << smax;
}