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
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 310;
const ll INF = 1e18;
int n, t;
vi V[nax];
int a[nax];
int sz[nax];
bool visited[nax];
ll small[nax], sum[nax];
vector<pair<ll, ll > >dp[nax][nax];
int naj;

bool cmp(pair<ll,ll>A, pair<ll,ll>B) {
	return A.ST + A.ND * A.ND < B.ST + B.ND * B.ND;
}

inline void dfs(int x) {
	visited[x] = 1;
	sz[x] = 1;
	sum[x] = a[x];
	dp[x][0].emplace_back(0, a[x]);
	for(int nbh : V[x]) {
		if(!visited[nbh]) {
			dfs(nbh);
			for(int s = sz[x] + sz[nbh] - 1; s >= 0; --s) {
				vector<pair<ll,ll> > tmp = {};
				int y = min(s+1, sz[x]);
				int y2 = max(0, s - sz[nbh] + 1);
				for(int i = y2; i < y; ++i) {
					for(auto &me : dp[x][i]) {
						for(auto &he : dp[nbh][s - i]) {
							tmp.emplace_back(he.ST + me.ST, he.ND + me.ND);
						}
					}
				}
				y = min(sz[x], s);
				y2 = max(0, s - sz[nbh]);
				for(int i = y2; i < y; ++i) {
					for(auto &me : dp[x][i]) {
						for(auto &he : dp[nbh][s - 1 - i]) {
							tmp.emplace_back(he.ST + me.ST + he.ND * he.ND, me.ND);
						}
					}
				}
				sort(tmp.begin(), tmp.end(), cmp);
				ll b = INF;
				int id = 0, ptr = 0;
				vi rem = {};
				//~ ll val = (ll)(sum[x] + sum[nbh]) / (s+1);
				//~ val *= val;
				//~ val *= 20;
				//~ if(s > n - 15) val = INF;
				for(auto &me : tmp) {
					if(me.ND >= b || ptr > 300) rem.PB(id);
					else {
						b = me.ND;
						ptr++;
					}
					id++;
				}
				for(int i = (int)rem.size() -1 ; i >= 0; --i) {
					swap(tmp[rem[i]], tmp.back());
					tmp.pop_back();
				}
				dp[x][s].swap(tmp);
			}
			sz[x] += sz[nbh];
			sum[x] += sum[nbh];
		}
	}
}

//~ ll all, cur;
//~ int K;

//~ void dfs2(int x) {
	//~ visited[x] = 1;
	//~ sum[x] = a[x];
	//~ for(int nbh : V[x]) if(!visited[nbh]) {
		//~ dfs2(nbh);
		//~ sum[x] += sum[nbh];
	//~ }
	//~ if(sum[x] * K >= all) {
		//~ cur += sum[x] * sum[x];
		//~ sum[x] = 0;
	//~ }
//~ }
			
int main() {_
	cin >> t;
	while(t--) {
		cin >> n;
		naj = 0;
		for(int i = 1; i <= n; ++i) {
			cin >> a[i];
			//~ all += a[i];
			naj = max(naj, a[i]);
			V[i].clear();
			for(int j = 0; j < n; ++j) {
				dp[i][j].clear();
			}
			visited[i] = 0;
		}
		for(int b,c,i = 1; i < n; ++i) {
			cin >> b >> c;
			V[b].PB(c);
			V[c].PB(b);
		}
		//~ for(int i = 1; i <= n; ++i) {
			//~ for(int j = 1; j <= n; ++j) {
				//~ visited[j] = 0;
			//~ }
			//~ K = i;
			//~ cur = 0;
			//~ dfs2(1);
			//~ cur += sum[1] * sum[1];
			//~ small[i - 1] = cur;
		//~ }
		//~ for(int i = 1; i <= n; ++i) {
			//~ visited[i] = 0;
		//~ }
		dfs(1);
		for(int i = 0; i < n; ++i) {
			ll ans = INF;
			for(auto &me : dp[1][i]) {
				ans = min(ans, me.ND * me.ND + me.ST);
			}
			//~ assert(ans != INF);
			cout << ans << " ";
		}
		cout << "\n";
		
	}
}