#P5865. [SEERC 2018] Tree

[SEERC 2018] Tree

Description

You are given a tree with nn vertices, numbered from 11 to nn. Each vertex is colored either black or white. Select exactly mm black vertices so that the maximum distance among all pairs of selected black vertices is minimized. Output this minimal possible maximum distance.

Input Format

The first line contains two integers nn and m (1mn100)m \ (1 \leq m \leq n \leq 100), representing the number of vertices in the tree and the number of black vertices to be selected.

The second line contains nn integers p1,p2,,pn (0pi1)p_1, p_2, \dots, p_n \ (0 \leq p_i \leq 1). If pi=1p_i = 1, then vertex ii is black; otherwise it is white. The testdata guarantees that there are at least mm black vertices.

The next n1n - 1 lines each contain two integers viv_i and ui (1vi,uin)u_i \ (1 \leq v_i, u_i \leq n), indicating that there is an edge between vertices viv_i and uiu_i in the tree. The testdata guarantees that these edges form a tree.

Output Format

Output one integer, which is the answer.

6 3
1 1 0 1 1 1
1 2
1 3
1 4
3 5
3 6
2
9 4
1 0 1 0 1 0 0 1 1
1 2
2 4
2 3
4 5
1 6
6 7
6 8
7 9
5

Hint

In the first sample, the only possible choice is to select vertices 11, 22, and 44, and the maximum distance is 22.

In the second sample, one feasible choice is to select vertices 11, 33, 88, and 99. The maximum distance is the distance between vertices 33 and 99.

Translated by ChatGPT 5