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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// tested by Hightail: https://github.com/dj3500/hightail
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int i=0;(i)<(n);++(i))
#define FORI(i,n) for(int i=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int n; scanf("%d",&(n));
#define wez2(n,m) int n,m; scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int n,m,k; scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#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 REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

const int N = 640;
ll a[N];
vi adj[N]; // initial input
int n;

int subtreeCount (int v, int p) {
   int cnt = 1;
   for (int x : adj[v]) if (x != p) cnt += subtreeCount(x,v);
   return cnt;
}

vector<pair<int,bool>> children[N];
void create (int v) {
   //DBG(mp(v,p))
   //DBG(adj[v])
   // TODO: optimize with more heuristics?
   
   // remove parent
   for (int x : adj[v]) {
      adj[x].erase(remove(adj[x].begin(), adj[x].end(), v), adj[x].end());
   }
   
   if (SZ(adj[v]) <= 2) {
      // just copy
      for (int x : adj[v]) {
         children[v].pb(mp(x,1));
      }
   } else {
      // pick largest subtree
      pii best(-INF,-1);
      for (int x : adj[v]) {
         REMAX(best, mp(subtreeCount(x,v), x));
      }
      const int largest = best.se;
      //DBG(largest);
      adj[v].erase(remove(adj[v].begin(), adj[v].end(), largest), adj[v].end());
      ++n;
      children[v].pb(mp(n,0));
      children[v].pb(mp(largest,1));
      a[n] = 0;
      adj[n] = adj[v];
   }
   
   for (auto it : children[v]) create(it.fi);
}

void clean (vector<pair<ll,ll>> &v) {
   sort(ALL(v));
   
   int j = 1;
   FOR(i,SZ(v)) {
      while (j < SZ(v) && v[i].fi*v[i].fi+v[i].se <= v[j].fi*v[j].fi+v[j].se) ++j;
      if (j == SZ(v)) {
         v.erase(v.begin() + i + 1, v.end());
         return;
      }
      if (i+1 < j) v[i+1] = v[j];
      ++j;
   }
   
   //vector<pair<ll,ll>> w; // TODO: in-place?
   // v = move(w);
}

vector<pair<ll,ll>> res[N][N];

void go (int v) {
   for (auto it : children[v]) go(it.fi);
   
   int maxK = 0;

   if (children[v].empty()) {
      res[v][1] = {{a[v],0}};
   } else if (SZ(children[v]) == 1) {
      assert(children[v][0].se == 1);
      const int x = children[v][0].fi;
      for (int k = 1; ; ++k) {
         if (res[x][k].empty()) break;
         for (const pair<ll,ll> &p : res[x][k]) {
            // do not cut
            res[v][k].emplace_back(p.fi + a[v], p.se);
            // cut
            res[v][k+1].emplace_back(a[v], p.se + p.fi*p.fi);
            REMAX(maxK, k+1);
         }
      }
      
      // clean
      REP(k,1,maxK) clean(res[v][k]);
   } else {
      if (SZ(children[v]) > 2) {
         DBG(SZ(children[v]));
      }
      assert(SZ(children[v]) == 2);
      const int x1 = children[v][0].fi, x2 = children[v][1].fi;
      for (int k1 = 1; ; ++k1) {
         if (res[x1][k1].empty()) break;
         for (int k2 = 1; ; ++k2) {
            if (res[x2][k2].empty()) break;
            
            for (const pair<ll,ll> &p1 : res[x1][k1]) {
               for (const pair<ll,ll> &p2 : res[x2][k2]) {
                  // cut neither
                  res[v][k1+k2-1].emplace_back(p1.fi + p2.fi + a[v], p1.se + p2.se);
                  // cut 1
                  if (children[v][0].se) {
                     res[v][k1+k2].emplace_back(a[v] + p2.fi, p1.se + p2.se + p1.fi*p1.fi);
                  }
                  // cut 2
                  if (children[v][1].se) {
                     res[v][k1+k2].emplace_back(a[v] + p1.fi, p1.se + p2.se + p2.fi*p2.fi);
                     // cut both
                     if (children[v][0].se) {
                        res[v][k1+k2+1].emplace_back(a[v], p1.se + p2.se + p1.fi*p1.fi + p2.fi*p2.fi);
                     }
                  }
                  REMAX(maxK, k1+k2+1);
               }
            }
         }
      }
      
      // clean
      REP(k,1,maxK) clean(res[v][k]);
   }
   
   // dealloc
   for (auto it : children[v]) {
      const int x = it.fi;
      REP(k,1,maxK) {
         vector<pair<ll,ll>>().swap(res[x][k]);
      }
   }
}

int main () {
   TESTS {
      FOR(i,N) {
         adj[i].clear();
         children[i].clear();
         FOR(j,N) res[i][j].clear();
      }
      scanf("%d", &n);
      FORI(i,n) {
         wez(ai);
         a[i] = ai;
      }
      FOR(i,n-1) {
         wez2(bi,ci);
         adj[bi].pb(ci);
         adj[ci].pb(bi);
      }
      
      // heuristic to find a good balanced root - minimize sum of squares of subtree sizes
      pii bestRoot(INF, -1);
      FORI(cand,n) {
         int sum = 0;
         for (int x : adj[cand]) {
            int subtree = subtreeCount(x,cand);
            sum += subtree * subtree;
         }
         REMIN(bestRoot, mp(sum, cand));
      }
      const int root = bestRoot.se;
      //DBG(root);
      
      const int oldN = n;
      
      create(root);
      //DBG("created");
      
      go(root);
      
      REP(k,1,oldN) {
         ll best = 1e18;
         for (pair<ll,ll> &p : res[root][k]) {
            REMIN(best, p.fi*p.fi + p.se);
         }
         cout << best << " ";
      }
      cout << "\n";
   }
}