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
#include <bits/stdc++.h>
using namespace std;
const long long INF=1000000000000000000;
const int SIZE=310;
void imax(int &a, int b){
	a=max(a, b);
}
void imin(int &a, int b){
	a=min(a, b);
}
void lmax(long long &a, long long b){
	a=max(a, b);
}
void lmin(long long &a, long long b){
	a=min(a, b);
}
/*
	WARNING: I'm using strange bracket style!
*/
struct Comparer {
    bool operator() (const pair <bitset<SIZE>, int> &a, const pair <bitset<SIZE>, int> &b) const {
		/*string A, B;
		for (int i=0; i<a.first.size(); i++)
			A+=a.first[i];
		for (int i=0; i<b.first.size(); i++)
			B+=b.first[i];
		A+=to_string(a.second);
		B+=to_string(b.second);
		return A<B;*/
		if (a.second!=b.second)
			return a.second<b.second;
		if (a.first.to_ulong()!=b.first.to_ulong())
			return a.first.to_ulong()<b.first.to_ulong();
		for (int i=0; i<a.first.size(); i++)
			if (a.first[i]!=b.first[i])
				return a.first[i]<b.first[i];
		return false;
	}
};
map <pair <bitset <SIZE>, int>, long long, Comparer> DP;
vector <int> graph[SIZE];
int lca[SIZE][SIZE];
bool visited[SIZE];
int deepth[SIZE];
int parent[SIZE];
int value[SIZE];
int siz[SIZE];
int x, y, n, q;
int slowLca(int x, int y){
	while (x!=y)
		if (deepth[x]>deepth[y])
			x=parent[x];
		else
			y=parent[y];
	return x;
}
int dist(int x, int y){
	return deepth[x]+deepth[y]-2*deepth[lca[x][y]]+69;
}
void dfs(int u){
	visited[u]=true;
	for (auto v: graph[u])
		if (!visited[v])
			parent[v]=u, deepth[v]=deepth[u]+1, dfs(v), siz[u]+=siz[v];
	siz[u]+=value[u];
}
void w(bitset <SIZE> M, string s){
	cout<<"{"<<M[1]<<" "<<M[2]<<" "<<M[3]<<" "<<M[4]<<" "<<M[5]<<" "<<M[6]<<" "<<M[7]<<"} "<<s<<endl;
}
long long run(bitset <SIZE> M, int k){
	if (DP[{M, k}]!=0)
		return DP[{M, k}];
	if (k==1)
		{
		long long ret=0;
		for (int i=1; i<=n; i++)
			if (M[i])
				ret+=value[i];
		return DP[{M, k}]=ret*ret;
		}
	long long X=INF;
	int trials=0;
	long double base=((long double)n-k)/n;
	long double act=1;
	for (int i=2; i<=n; i++)
		if (M[parent[i]] && M[i])
			{
			if (k>10 && act<0.09)
				break;
			if (k>4 && k<=10 && act<0.001)
				break;
			if (k<=4 && act<0.000001)
				break;
			act*=base, trials++, base=((long double)n-k-trials)/n;
			pair <int, int> edge={i, parent[i]};
			int c1=0, c2=0;
			long long A=0, B=0;
			bitset <SIZE> B1, B2;
			for (int i=1; i<=n; i++)
				if (M[i])
					{
					if (dist(edge.first, i)>dist(edge.second, i))
						A+=value[i], B1[i]=true, c1++;
					else
						B+=value[i], B2[i]=true, c2++;
					}
			int k1=((long double)A*k)/(A+B);
			int k2=k-k1;
			if (k1>c1)
				k1=c1, k2=k-k1;
			if (c2<k2)
				k2=c2, k1=k-k2;
			if (k1==0)
				k1++, k2--;
			if (k2==0)
				k1--, k2++;
			lmin(X, run(B1, k1)+run(B2, k2));
			if (k1-1>0 && k2+1<=c2)
				lmin(X, run(B1, k1-1)+run(B2, k2+1));
			if (k1+1<=c1 && k2-1>0)
				lmin(X, run(B1, k1+1)+run(B2, k2-1));
			}
	return DP[{M, k}]=X;
}
int main()
	{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>q;
	while (q--)
		{
		cin>>n;
		for (int i=1; i<=n; i++)
			cin>>value[i];
		for (int i=1; i<n; i++)
			cin>>x>>y, graph[x].push_back(y), graph[y].push_back(x);
		dfs(1);
		for (int i=1; i<=n; i++)
			for (int j=1; j<=n; j++)
				lca[i][j]=slowLca(i, j);
		bitset <SIZE> fullMask;
		for (int i=1; i<=n; i++)
			fullMask[i]=true;
		for (int i=1; i<=n; i++)
			cout<<run(fullMask, i)<<" ";
		cout<<"\n";
		DP.clear(), x=0, y=0, n=0;
		for (int i=0; i<SIZE; i++)
			siz[i]=value[i]=parent[i]=deepth[i]=visited[i]=0, graph[i].clear();
		for (int i=0; i<SIZE; i++)
			for (int j=0; j<SIZE; j++)
				lca[i][j]=0;
		}
	return 0;
	}