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

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ST first
#define ND second
#define PB push_back
#define SIZE(a) ((int)a.size())

template<class T, class U>
ostream& operator<<(ostream &stream, pair<T, U> &p) {
    stream << "(" << p.ST << "," << p.ND << ")";
    return stream;
}

template<class T>
ostream& operator<<(ostream &stream, vector<T> &v) {
    stream << "[";
    for(auto elem : v) {
        stream << elem << ", ";
    }
    stream << "]";
    return stream;
}

typedef vector<vector<pll>> vvpll;

const int maxn = 310;
vector<int> v[maxn];
int a[maxn];

vvpll delete_dominated(vvpll &sub) {
    vvpll res;
    double sub_count=0, res_count=0;
    for(int i=SIZE(sub)-1; i >= 0; i--) {
        vector<pll> &v = sub[i];
        sort(v.begin(), v.end(), [](pll &a, pll &b){return a > b;});
        vector<pll> u, z;
        for(pll x : v) {
            while(!u.empty() && u.back().ST >= x.ST && u.back().ND >= x.ND) {
                u.pop_back();
            }
            u.PB(x);
        }
        ll minv = u.back().ST*u.back().ST+u.back().ND;
        for(int j=SIZE(u)-1; j >= 0; j--) {
            ll val = u[j].ST*u[j].ST+u[j].ND;
            if(val <= minv) {
                z.PB(u[j]);
            }
        }
        res.PB(z);
        sub_count+=SIZE(v);
        res_count+=SIZE(z);
    }
    reverse(res.begin(), res.end());
    // cerr << sub_count << "/" << res_count << "=" << sub_count/res_count << "\n";
    return res;
}

vvpll dfs(int q, int p) {
    vvpll res(1);
    res[0].PB({ll(a[q]), 0});
    for(int b : v[q]) {
        if(b == p) {
            continue;
        }
        vvpll sub = dfs(b, q);
        vvpll tmp(SIZE(sub)+SIZE(res)-1);
        for(int i=0; i < SIZE(res); i++) {
            for(int j=0; j < SIZE(sub); j++) {
                for(pll x : res[i]) {
                    for(pll y : sub[j]) {
                        tmp[i+j].PB({x.ST+y.ST, x.ND+y.ND});
                    }
                }
            }
        }
        res = delete_dominated(tmp);
    }
    res.PB({});
    for(int i=SIZE(res)-2; i >= 0; i--) {
        for(pll x : res[i]) {
            res[i+1].PB({0, x.ST*x.ST+x.ND});
        }
    }
    // cerr << q << ": " << SIZE(res) << "\n";
    // cerr << res << "\n\n";
    return delete_dominated(res);
}

int main() {
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        for(int i=1; i <= n; i++) {
            cin >> a[i];
            v[i].clear();
        }
        for(int i=0; i < n-1; i++) {
            int b, c;
            cin >> b >> c;
            v[b].PB(c);
            v[c].PB(b);
        }
        auto res = dfs(1, -1);
        for(int i=1; i <= n; i++) {
            cout << res[i][0].ND << " ";
        }
        cout << "\n";
    }
}