Under construction

Still being built. Things may change

Watching Cowflix

Time limit3sMemory limit1024 MB

Summary
Given a tree with marked nodes, find for each k from 1 to N the minimum total cost of disjoint connected components covering all marked nodes, where a component of size d costs d+k.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Divide and conquer
Solved
No attempts yet

Statement

Bessie likes watching shows on Cowflix, and she watches them in different places. Farmer John's farm is a tree with NN (2≤N≤2⋅1052 \leq N \leq 2\cdot 10^5) nodes. Each node is either a place where Bessie watches Cowflix or a place where she does not. At least one node is a place where Bessie watches Cowflix.

Cowflix is introducing a new subscription model to combat password sharing. Under the new model, you choose a connected component of size dd in the farm, and you pay d+kd + k moonies for an account that works in that component. Formally, choose a set of disjoint connected components c1,c2,…,cCc_1, c_2, \dots, c_C so that every node where Bessie watches Cowflix is contained in some cic_i. The cost of the set is ∑i=1C(∣ci∣+k)\sum_{i=1}^{C} (|c_i|+k), where ∣ci∣|c_i| is the number of nodes in cic_i. Nodes where Bessie does not watch Cowflix do not have to be in any cic_i.

Bessie is worried the new model may be too expensive for her viewing habits, and she is considering switching to Mooloo. To help her decide, calculate the minimum amount she must pay Cowflix to keep her viewing habits. Cowflix has not announced kk, so calculate the answer for every integer kk from 11 to NN.

Input

The first line contains NN. The second line contains a bit string s1s2s3…sNs_1s_2s_3 \dots s_N, where si=1s_i = 1 if Bessie watches Cowflix at node ii. Then N−1N-1 lines follow, each with two integers aa and bb (1≤a,b≤N1 \leq a, b \leq N), denoting an edge between aa and bb in the tree.

Output

Print the answers for each kk from 11 to NN, one per line.

Examples2

  1. Example 1

    Input
    5
    10001
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    4
    6
    8
    9
    10
    
  2. Example 2

    Input
    7
    0001010
    7 4
    5 6
    7 2
    5 1
    6 3
    2 5
    
    Expected output
    4
    6
    8
    9
    10
    11
    12