1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // XIII LO Szczecin
using LL = long long;    // Poland
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
// end of templates

struct Solve {
	int n;
	vector<int> a;
	vector<vector<int>> graph;
	LL sum_a = 0;

	using State = pair<int, LL>;
	using States = vector<pair<int, LL>>;

	vector<vector<States>> dp;

	LL curr_score(pair<int, LL> p) {
		return p.first * LL(p.first) + p.second;
	}

	void compress_states(int k, States &vec) {
		sort(vec.begin(), vec.end());
		vector<pair<int, LL>> ret;
		for(auto [sum, ans] : vec)
			//if(size(ret) != 0 and (sum <= ret.back().first or score(make_pair(sum, ans)) <= score(ret.back())))
			if(size(ret) != 0 and (ans >= ret.back().second or curr_score(make_pair(sum, ans)) >= curr_score(ret.back())))
				continue;
			else
				ret.emplace_back(sum, ans);
		vec = ret;

		/*
		sort(vec.begin(), vec.end(), [&](pair<int, LL> x, pair<int, LL> y) {
			LL expected = k == 0 ? 0LL : (sum_a * sum_a / k);
			LL sx = x.first * LL(x.first) + x.second,
			   sy = y.first * LL(y.first) + y.second;
			// return abs(sx - expected) < abs(sy - expected);
			return sx < sy;
			// return x.first < y.first;
			// return x.second > y.second;
		});
		//constexpr int limit = 10000;
		//if(size(vec) > limit)
			//vec.resize(limit);
		*/
	}

	vector<States> merge(vector<States> &x, vector<States> &y) {
		// cerr << size(x) << ' ' << size(y) << '\n';
		vector<States> ret(size(x) + size(y) - 1);
		REP(k1, size(x))
			REP(k2, size(y))
				for(auto [sum1, ans1] : x[k1])
					for(auto [sum2, ans2] : y[k2]) {
						State new_val = {sum1 + sum2, ans1 + ans2};
						int new_k = k1 + k2;
						ret[new_k].emplace_back(new_val);
					}
		REP(k, size(ret))
			compress_states(k, ret[k]);
		return ret;
	}

	void dfs(int v = 0, int p = -1) {
		vector<States> merged_sons = {{{0, 0}}};
		for(int u : graph[v])
			if(u != p) {
				dfs(u, v);
				merged_sons = merge(merged_sons, dp[u]);
				REP(k, size(merged_sons))
					compress_states(k, merged_sons[k]);
			}
		debug(v, p, merged_sons);

		dp[v].resize(size(merged_sons) + 1);
		REP(k, size(merged_sons))
			for(auto [sum, ans] : merged_sons[k]) {
				int s = sum + a[v];
				dp[v][k + 1].emplace_back(0, ans + s * LL(s));
				dp[v][k].emplace_back(s, ans);
			}
		REP(k, size(dp[v]))
			compress_states(k, dp[v][k]);
		debug(v, dp[v]);
	}

	Solve() {
		cin >> n;
		a.resize(n);
		for(int &ai : a)
			cin >> ai;
		graph.resize(n);
		REP(edge, n - 1) {
			int v, u;
			cin >> v >> u;
			--v, --u;
			graph[v].emplace_back(u);
			graph[u].emplace_back(v);
		}
		sum_a = accumulate(a.begin(), a.end(), 0LL);

		dp.resize(n);
		dfs();
		FOR(k, 0, n - 1) {
			vector<LL> all_answers;
			for(auto [sum, ans] : dp[0][k])
				all_answers.emplace_back(sum * LL(sum) + ans);
			assert(size(all_answers));
			cout << *min_element(all_answers.begin(), all_answers.end()) << " ";
		}
		cout << '\n';
	}
};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while(t --> 0)
		Solve();
}