Watching Cowflix

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Bessie likes to watch shows on Cowflix, and she watches them in different places. Farmer John's farm can be represented as a tree with NN (2N21052 \leq N \leq 2\cdot 10^5) nodes, and for each node, either Bessie watches Cowflix there or she doesn't. It is guaranteed that Bessie watches Cowflix in at least one node.

Unfortunately, Cowflix is introducing a new subscription model to combat password sharing. In their new model, you can choose a connected component of size dd in the farm, and then you need to pay d+kd + k moonies for an account that you can use in that connected component. Formally, you need to choose a set of disjoint connected components c_1,c_2,,c_Cc\_1, c\_2, \dots, c\_C so that every node where Bessie watches Cowflix must be contained within some c_ic\_i. The cost of the set of components is _i=1C(c_i+k)\sum\_{i=1}^{C} (|c\_i|+k), where c_i|c\_i| is the number of nodes in component c_ic\_i. Nodes where Bessie does not watch Cowflix do not have to be in any c_ic\_i.

Bessie is worried that the new subscription model may be too expensive for her given all the places she visits and is thinking of switching to Mooloo. To aid her decision-making, calculate the minimum amount she would need to pay to Cowflix to maintain her viewing habits. Because Cowflix has not announced the value of kk, calculate it for all integer values of kk from 11 to NN.

입력

The first line contains NN.

The second line contains a bit string s_1s_2s_3s_Ns\_1s\_2s\_3 \dots s\_N where s_i=1s\_i = 1 if Bessie watches Cowflix at node ii.

Then N1N-1 lines follow, each containing two integers aa and bb (1a,bN1 \leq a, b \leq N), which denotes an edge between aa and bb in the tree.

출력

The answers for each kk from 11 to NN on separate lines.