Note: The time limit for this problem is 3s, 1.5x the default.
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 N (2≤N≤2⋅105) 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 d in the farm, and then you need to pay d+k moonies for an account that you can use in that connected component. Formally, you need to choose a set of disjoint connected components c1,c2,…,cC so that every node where Bessie watches Cowflix must be contained within some ci. The cost of the set of components is ∑Ci=1(|ci|+k), where |ci| is the number of nodes in component ci. Nodes where Bessie does not watch Cowflix do not have to be in any ci.
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 k, calculate it for all integer values of k from 1 to N.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains N.
The second line contains a bit string s1s2s3…sN where si=1 if Bessie watches Cowflix at node i.
Then N−1 lines follow, each containing two integers a and b (1≤a,b≤N), which denotes an edge between a and b in the tree.
OUTPUT FORMAT (print output to the terminal / stdout):
The answers for each k from 1 to N on separate lines.
SAMPLE INPUT:
5
10001
1 2
2 3
3 4
4 5
SAMPLE OUTPUT:
4
6
8
9
10
For k≤3, it's optimal to have two accounts: c1={1},c2={5}. For k≥3, it's optimal to have one account: c1={1,2,3,4,5}.
SAMPLE INPUT:
7
0001010
7 4
5 6
7 2
5 1
6 3
2 5
SAMPLE OUTPUT:
4
6
8
9
10
11
12
SCORING:
Inputs 3-5: N≤5000
Inputs 6-8: i is connected to i+1 for all i∈[1,N).
Inputs 9-19: N≤105
Inputs 20-24: No additional constraints.
Problem credits: Danny Mittal