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
#include <bits/stdc++.h>
using namespace std;


// https://github.com/kth-competitive-programming/kactl/blob/master/content/data-structures/LineContainer.h
using ll = long long;

struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
        ll operator()(ll x) const { return k * x + m; }
};

struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
                // cerr << " >>> ADDING : " << k << " " << m << endl;
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
	Line query_line(ll x) {
		assert(!empty());
		return *lower_bound(x);
	}
	auto query_iterator(ll x) {
		assert(!empty());
		return lower_bound(x);
	}
};

struct TestCase {
    int n;
    vector<long long> weights;
    vector<vector<int>> graph;
    vector<int> subtree_size;
    vector<vector<LineContainer>> dp;

    static constexpr int ROOT = 0;
    static constexpr int ITERS = 8;

    void compute_dp(int v = ROOT, int parent = -1) {
        subtree_size[v] = 1;
        dp[v].resize(subtree_size[v] + 1);
        dp[v][1].add(-2 * weights[v], -weights[v] * weights[v]);

        for (auto u : graph[v]) {
            if (u != parent) {
                compute_dp(u, v);
                vector<LineContainer> next_dp(subtree_size[v] + subtree_size[u] + 1);
                for (int x = 1; x <= subtree_size[v]; x++) {
                    for (int y = 1; y <= subtree_size[u]; y++) {
                        for (auto my_line : dp[v][x]) {
                            for (auto line : dp[u][y]) {
                                // everything negative
                                auto weight = -line.k / 2;
                                auto cost = my_line(weight) + line.m;
                                auto total_weight = -weight + my_line.k / 2;
                                next_dp[x + y].add(my_line.k, my_line.m + line.m);
                                next_dp[x + y - 1].add(2 * total_weight, cost);
                            }
                        }
                        /*
                        for (auto line : dp[u][y]) {
                            // everything negative
                            auto weight = -line.k / 2;

                            auto it = dp[v][x].query_iterator(weight);
                            for (int i = 0; i < ITERS; i++) {
                                if (it != dp[v][x].begin()) {
                                    --it;
                                } else {
                                    break;
                                }
                            }
                            for (int i = 0; i < 2 * ITERS; i++) {
                                if (it == dp[v][x].end())
                                    break;
                                auto my_line = *it;
                                auto cost = my_line(weight) + line.m;
                                auto total_weight = -weight + my_line.k / 2;
                                next_dp[x + y].add(my_line.k, my_line.m + line.m);
                                next_dp[x + y - 1].add(2 * total_weight, cost);
                                ++it;
                            }
                        }
                        */
                    }
                }
                dp[v] = move(next_dp);
                subtree_size[v] += subtree_size[u];

                /*
                cerr << " [NEXT_DP " << u + 1 << "] NODE : " << v + 1 << endl;
                for (int i = 1; i <= subtree_size[v]; i++) {
                    cerr << "   " << i << endl;
                    for (auto line : dp[v][i]) {
                        cerr << "    " << line.k << " " << line.m << endl;
                    }
                }
                */
            }
        }

        /*
        cerr << " NODE : " << v + 1 << endl;
        for (int i = 1; i <= subtree_size[v]; i++) {
            cerr << "   " << i << endl;
            for (auto line : dp[v][i]) {
                cerr << "    " << line.k << " " << line.m << endl;
            }
        }
        */
    }

    void solve() {
        cin >> n;
        weights.resize(n);
        for (auto& x : weights) { 
            cin >> x;
        }

        graph.resize(n);
        for (int i = 0; i < n - 1; i++) {
            int b, c;
            cin >> b >> c;
            b--, c--;
            graph[b].push_back(c);
            graph[c].push_back(b);
        }

        subtree_size.resize(n);
        dp.resize(n);
        compute_dp();

        for (int i = 1; i <= n; i++) {
            long long mn = numeric_limits<long long>::max();
            for (auto line : dp[ROOT][i]) {
                mn = min(mn, -line.m);
            }
            // long long mn = -dp[ROOT][i].begin()->m;
            cout << mn << " ";
        }
        cout << "\n";
    }
};

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

    int T;
    cin >> T;
    while (T--) {
        TestCase().solve();
    }
}