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

using namespace std;

#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define ll long long
int mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;

vector<int> G[307];
ll a[307],cnt,ans[307];
bool odw[307];
pair<int,int>kraw[307];
int n;
void dfs(int v)
{
    cnt+=a[v];
    odw[v]=1;
    for(auto u:G[v]) if(!odw[u]) dfs(u);
}
void bt(int i)
{
    if(i==n)
    {
        int c=0;
        ll act=0;
        for(int i=1;i<=n;i++) odw[i]=0;
        for(int i=1;i<=n;i++)
        {
            if(!odw[i])
            {
                cnt=0;
                dfs(i);
                act+=cnt*cnt;
                c++;
            }
        }
        ans[c]=min(ans[c],act);
        return ;
    }
    int u=kraw[i].st,v=kraw[i].nd;
    G[u].pb(v);
    G[v].pb(u);
    bt(i+1);
    G[u].pop_back();
    G[v].pop_back();
    bt(i+1);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++) ans[i]=infl;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<n;i++) cin>>kraw[i].st>>kraw[i].nd;
        bt(1);
        for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
        cout<<endl;
    }


    return 0;
}

/*#include <bits/stdc++.h>
using namespace std;

int random_int(int a, int b) { return a + rand() % (b - a + 1); }

int ans=0;

void random_tree(int n) {
  vector<int> l1, l2;
  vector<pair<int, int>> edges;

  for (int i = 0; i < n - 2; i++)
    l1.push_back(random_int(1, n));
  for (int i = 0; i < n; i++)
    l2.push_back(i + 1);

  while ((int)l2.size() > 2) {
    for (int i : l2) {
      if (find(l1.begin(), l1.end(), i) != l1.end()) {
        continue;
      }

      edges.push_back({i, l1[0]});
      l1.erase(l1.begin());
      l2.erase(find(l2.begin(), l2.end(), i));
      break;
    }
  }
  edges.push_back({l2[0], l2[1]});
  for (int i = 0; i < (int)edges.size(); ++i) {
    swap(edges[i], edges[random_int(0, i)]);
  }
  int ile[n+7];
  for(int i=1;i<=n;i++) ile[i]=0;
  for (pair<int, int> i : edges) {
    if (random_int(0, 1) == 1)
      swap(i.first, i.second);
  //  printf("%d %d\n", i.first, i.second);
    ile[i.first]++;
    ile[i.second]++;
  }
  int mxi=0;
  for(int i=1;i<=n;i++)ans=max(ans,ile[i]);
  //cout<<mxi<<endl;
}

int main() {
  int seed;
  cin >> seed;
  srand(seed);

  int t;
  cin >> t;
 // assert(1 <= t && t <= 10);

  int min_n, max_n;
  cin >> min_n >> max_n;
  assert(2 <= min_n && min_n <= max_n && max_n <= 300);

  /*int max_a_i;
  cin >> max_a_i;
  assert(1 <= max_a_i && max_a_i <= 1000 * 1000);

 // printf("%d\n", t);
  while (t--) {

    int n = random_int(min_n, max_n);
   // printf("%d\n", n);
   // for (int i = 1; i <= n; i++)
      //printf("%d%c", random_int(1, max_a_i), " \n"[i == n]);
    random_tree(n);
  }
  cout<<ans;
  return 0;
}
*/