Cod sursa(job #1616958)

Utilizator TeodorCotetCotet Teodor TeodorCotet Data 27 februarie 2016 12:20:07
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 16000;
const int INF = 0x7fffffff;

int n; 

int cost[NMAX + 1];

int best[NMAX + 1]; //best[i] = cel mai bun cost care se poate forma cu un subarbore cu radacina in i, care il cuprinde pe i
					//considerand ca radacina este in nodul 1

vector<int> g[NMAX + 1];

int smax = -INF;

void read() {

	fin >> n; 

	for(int i = 1; i <= n ; ++i)
		fin >> cost[i];

	for(int i = 1; i < n ; ++i) {

		int x; int y;
		fin >> x >> y;

		g[x].push_back(y);
		g[y].push_back(x);

	}
}

void dfs(int node, int t) {

	for(int x : g[node])
		if(x != t)
			dfs(x, node);

	best[node] = cost[node];

	for(int x : g[node])
		if(x != t && best[x] > 0)
			best[node] += best[x];

	if(best[node] > smax) smax = best[node];

}

void solve() {

	dfs(1, 0);

	fout << smax << '\n'; 

}

int main() {

	read();

	solve();

	return 0;
}