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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second
typedef pair<ll,int> pli;

const int N = 333;
const int infi = 1000100200;
const ll infll = 1000100200300400500LL;

struct Answer {
	ll s;
	int w;
	vector<pli> all;
	Answer() { s=infll; w=infi; all.emplace_back(s,w); }
	Answer(ll s_, int w_) { s=s_; w=w_; all.emplace_back(s,w); }
	Answer(vector<pli> tmp) {
		all = tmp;
		recalc();
	}
	void recalc() {
		vector<pli> tmp = all;
		sort(tmp.begin(), tmp.end());
		int pos = 0, cnt = 1;
		for (int i = 1; i < SZ(tmp); i++) {
			if (tmp[i].se < tmp[pos].se) {
				pos = i;
				cnt++;
			}
		}
		s = tmp[0].fi;
		w = tmp[0].se;
		all.resize(cnt);
		all[0] = tmp[0];
		cnt = 1; pos = 0;
		for (int i = 1; i < SZ(tmp); i++) {
			if (tmp[i].se < tmp[pos].se) {
				all[cnt] = tmp[i];
				pos = i;
				cnt++;
			}
		}
		//cerr << "\t\t\t\t";
		//for (auto x : all) cerr << x.fi << "\t";
		//cerr << "\n\t\t\t\t";
		//for (auto x : all) cerr << x.se << "\t";
		//cerr << "\n\t\t\t\t\ttmp was ";
		//for (auto x : tmp) cerr << "(" << x.fi << " " << x.se << ")\t";
		//cerr << "\n\n";
	}
	void update(Answer a) {
		if (a.s < s) {
			s = a.s;
			w = a.w;
		}
		all.insert(all.end(), a.all.begin(), a.all.end());
		recalc();
	}
	Answer add(ll s0) {
		Answer res(*this);
		res.s += s0;
		for (auto &x : res.all) x.fi += s0;
		return res;
	}
	Answer combine(Answer a) {
		//cerr << "\t\t\t";
		//for (auto x : all)   cerr << x.fi << "\t";
		//cerr << "\\/\t";
		//for (auto x : a.all) cerr << x.fi << "\t";
		//cerr << "\t_\n\t\t\t";
		//for (auto x : all)   cerr << x.se << "\t";
		//cerr << "/\\\t";
		//for (auto x : a.all) cerr << x.se << "\t";
		//cerr << "\t-\n";
		//return Answer(s+a.s+2LL*w*a.w, w+a.w);
		vector<pli> tmp;
		tmp.reserve(SZ(all)*SZ(a.all));
		for (auto x : all) for (auto y : a.all) {
			tmp.emplace_back(x.fi + y.fi + 2LL*x.se*y.se, x.se + y.se);
		}
		//return Answer(tmp[0].fi, tmp[0].se);
		return Answer(tmp);
	}
};

int n, kk;
int a[N];
int siz[N], sum[N], ord[N], par[N];
vector<int> g[N];

Answer dp[N][N];

void calc(int u) {
	//cerr << "Tree " << u+1 << "[" << a[u] << "] : \n";
	Answer dpc[N], dpp[N];
	dpp[1] = Answer(1LL*a[u]*a[u], a[u]);
	FOR(i,SZ(g[u])) {
		int v = g[u][i];
		if (v==par[u]) continue;
		for (int ks = 1; ks <= siz[u]; ks++) {
			for (int k = 1; k <= siz[v] && k <= ks; k++) {
				ll s0 = dp[v][k].s, s1 = dpp[ks-k].s, s2 = dpp[ks-k+1].s;
				int w0 = dp[v][k].w, w1 = dpp[ks-k].w, w2 = dpp[ks-k+1].w;
				//cerr << "\t\tUsing dp[" << v << "][" << k << "] = " << s0 << ", " << w0 << " to update ks = " << ks << "\n";
				//cerr << "\t\tOption 1: " << s1 << ", " << w1 << "\n";
				//cerr << "\t\tOption 2: " << s2 << ", " << w2 << "\n";
				dpc[ks].update(dpp[ks-k].add(s0));
				//Answer(s1 + s0, w1));
				dpc[ks].update(dpp[ks-k+1].combine(dp[v][k]));
				//Answer(s2 + s0 + 2LL*w2*w0, w2 + w0));
			}
		}
		//cerr << "\tAfter processing " << v+1 << " the options are \n\tk = \t";
		//for (int ks = 1; ks <= siz[u]; ks++) cerr << ks << "\t"; cerr << "\n\ts = \t";
		//for (int ks = 1; ks <= siz[u]; ks++) cerr << dpc[ks].s << "\t"; cerr << "\n\tw = \t";
		//for (int ks = 1; ks <= siz[u]; ks++) cerr << dpc[ks].w << "\t"; cerr << "\n\n";
		for (int ks = 1; ks <= siz[u]; ks++) {
			dpp[ks]=dpc[ks];
			dpc[ks]=Answer();
		}
	}
	for (int ks = 1; ks <= siz[u]; ks++) {
		dp[u][ks] = dpp[ks];
	}
	//cerr << "Final options are\n\tk = \t";
	//for (int ks = 1; ks <= siz[u]; ks++) cerr << ks << "\t"; cerr << "\n\ts = \t";
	//for (int ks = 1; ks <= siz[u]; ks++) cerr << dp[u][ks].s << "\t"; cerr << "\n\tw = \t";
	//for (int ks = 1; ks <= siz[u]; ks++) cerr << dp[u][ks].w << "\t"; cerr << "\n\n";
}

void dfs_cnt(int u, int pp) {
	siz[u] = 1;
	sum[u] = a[u];
	ord[--kk] = u;
	par[u] = pp;
	FOR(i,SZ(g[u])) {
		int v = g[u][i];
		if (v!=pp) {
			dfs_cnt(v,u);
			siz[u] += siz[v];
			sum[u] += sum[v];
		}
	}
}

void clear() {
	kk = n;
	FOR(i,n) g[i].clear();
}

void test() {
	scanf("%d", &n);
	clear();
	FOR(i,n) scanf("%d", &a[i]);
	FOR(i,n-1) {
		int b,c;
		scanf("%d%d", &b, &c);
		b--; c--;
		g[b].push_back(c);
		g[c].push_back(b);
	}
	dfs_cnt(0,-1);
	FOR(ii,n) {
		int u = ord[ii];
		calc(u);
	}
	//FORI(i,n) printf("%.2lf ", 1.0*dp[0][i].s*i/sum[0]/sum[0]);
	FORI(i,n) printf("%lld ", dp[0][i].s);
	printf("\n");
}

int main() {
	int tt;
	scanf("%d", &tt);
	while (tt--) test();
	return 0;
}