Cod sursa(job #2737590)

Utilizator Gota_AndreiGota Andrei Gota_Andrei Data 4 aprilie 2021 21:17:07
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.53 kb
#include<bits/stdc++.h>
using namespace std;

int n, v[16005], maxx = INT_MIN;
bitset <16005> viz;
vector <int> nod[16005];

void dfs(int i) {
	for (int itr : nod[i])
		if (!viz[itr]) {
			viz[itr] = true;
			dfs(itr);
			v[i] = max(v[i], v[i] + v[itr]);
		}

	maxx = max(maxx, v[i]);
}

int main () {
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> v[i];

	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		nod[x].push_back(y);
		nod[y].push_back(x);
	}

	viz[1] = true;

	dfs(1);

	cout << maxx;
}