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
#include <iostream>
#include <vector>
#include <stack>
#include <math.h> 

using namespace std;

int min(int a, int b)
{
    return a < b ? a : b;
}

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

    int n, m, i, j, k;

    cin >> n;

    while (n--)
    {
        cin >> m;

        int* miasta = new int[m];
        float wholeSum = 0;
        for (i = 0; i < m; i++)
        {
            cin >> miasta[i];
            wholeSum += miasta[i];
        }

        vector<int>* drogiz = new vector<int>[m];
        int a, b;
        for (i = 0; i < m - 1; i++)
        {
            cin >> a;
            a--;
            cin >> b;
            b--;

            drogiz[a].push_back(b);
            drogiz[b].push_back(a);
        }

        //nadanie kazdemu miastu jego rodzica

        stack<pair<int, int>> toAssign; //from - to
        toAssign.push({ -1, 0 });
        pair<int, int> elem;
        int* parent = new int[m];
        vector<int>* kids = new vector<int>[m];

        parent[0] = -1;
        while (!toAssign.empty())
        {
            elem = toAssign.top();
            toAssign.pop();

            for (auto it : drogiz[elem.second])
            {
                if (it != elem.first)
                {
                    kids[elem.second].push_back(it);

                    toAssign.push({ elem.second, it });
                    parent[it] = elem.second;
                }
            }
        }

        //zsumowanie miast, zapisanie jako wzor dla kazdego z przykladow

        int* miastaVal = new int[m];

        for (i = 0; i < m; i++)
        {
            miastaVal[i] = 0;
        }

        int plus, paren;
        for (i = 0; i < m; i++)
        {
            plus = miasta[i];
            paren = i;

            while (paren != -1)
            {
                miastaVal[paren] += plus;

                paren = parent[paren];
            }
        }

        
        int searchedVal;
        int buff3, buff4;

        int* miastaValBuff = new int[m]; //miasto - wartosc
        int* parentBuff = new int[m];

        pair<int, int> closestVal;
        vector<int> parentNodes;
        int subTreeVal;
        stack<int> toBuff;
        int buff5;

        for (i = 0; i < m; i++) //ilosci spalonych mostow
        {
            for (j = 0; j < m; j++)
            {
                miastaValBuff[j] = miastaVal[j];
                parentBuff[j] = parent[j];
            }

            parentNodes.clear();
            parentNodes.push_back(0);

            searchedVal = round(wholeSum / (i + 1));
            

            for (j = 0; j < i; j++) //spalenia kazdego mostu po kolei
            {
                closestVal = { -1, -1 }; //miasto - odleglosc

                
                for (auto it : parentNodes)
                {
                    subTreeVal = miastaValBuff[it];

                    toBuff.push(it);

                    while (!toBuff.empty())
                    {
                        buff5 = toBuff.top();
                        toBuff.pop();

                        for (auto jt : kids[buff5])
                        {
                            if (parentBuff[jt] != -1)
                            {
                                buff3 = min(miastaValBuff[jt], (subTreeVal - miastaValBuff[jt]));
                                buff3 = abs(searchedVal - buff3);

                                if (buff3 < closestVal.second || closestVal.second == -1)
                                {
                                    closestVal.first = jt;
                                    closestVal.second = buff3;
                                }

                                toBuff.push(jt);
                            } 
                        }
                    }
                }

                paren = parentBuff[closestVal.first];
                buff4 = miastaValBuff[closestVal.first];

                while (paren != -1) //po wybraniu miasta przejscie przez kazde miasto rodzicowskie i odejmowanie
                {
                    miastaValBuff[paren] -= buff4;

                    paren = parentBuff[paren];
                }

                parentBuff[closestVal.first] = -1;
                parentNodes.push_back(closestVal.first);
            }

            long long wynik = 0;
            for (auto it : parentNodes)
            {
                wynik += (long long)miastaValBuff[it] * miastaValBuff[it];
            }

            cout << wynik << " ";
        }
        cout << endl;
    }
}