Cod sursa(job #2704858)

Utilizator MoiseVictor123Moise victor MoiseVictor123 Data 11 februarie 2021 15:08:33
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream in("asmax.in");
ofstream out("asmax.out");

const int N = 16000;

int n;
vector<int> a[N + 1];
bool viz[N + 1];
int sol[N + 1];

void dfs(int x) {
	viz[x] = true;
	for (auto i : a[x]) {
		if (!viz[i]) {
			dfs(i);
			if(sol[i] > 0)
				sol[x] += sol[i];
		}
	}
}

int main() {
	in >> n;
	for(int i = 1; i <= n; i++)
		in >> sol[i];
	for (int i = 0; i < n - 1; i++) {
		int x;
		int y;
		in >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	dfs(1);
	int maxi = INT_MIN;
	for (int i = 1; i <= n; i++) {
		maxi = max(maxi, sol[i]);
	}
	out << maxi;
}