Cod sursa(job #2928967)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 24 octombrie 2022 12:29:28
Problema Asmax Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpl = vector<pl>;
using vti = vector<ti>;
using vtl = vector<tl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvpil = vector<vpil>;
using vvpli = vector<vpli>;
using vvpl = vector<vpl>;
using vvti = vector<vti>;
using vvtl = vector<vtl>;


#if 1
ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define cin fin
#define cout fout
#endif
const int nmx = 16e3+1;
vi a[nmx];
int v[nmx],s[nmx];

void Dfs(int k,int t)
{
	for (int to : a[k])
	{
		if (to != t)
		{
			Dfs(to,k);
			s[k] += s[to];
		}
	}
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> v[i], s[i] = v[i];
	for (int i = 0; i < n - 1; i++)
	{
		int x, y;
		cin >> x >> y;
		a[x].emplace_back(y);
		a[y].emplace_back(x);
	}
	int mx = INT_MIN;
	for (int i = 1; i <= n; i++)
	{
		for (int i = 1; i <= n; i++)
			s[i] = v[i];
		Dfs(i, 0);
		for (int i = 1; i <= n; i++)
			mx = max(mx, s[i]);

	}
	cout << mx;
}