QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 64 MB Total points: 100

#316. Dynamite

الإحصائيات

The Byteotian Cave is composed of $n$ chambers and $n-1$ corridors that connect them. For every pair of chambers there is unique way to move from one of them to another without leaving the cave. Dynamite charges are set up in certain chambers. A fuse is laid along every corridor. In every chamber the fuses from the adjacent corridors meet at one point, and are further connected to the dynamite charge if there is one in the chamber. It takes exactly one unit of time for the fuse between two neighbouring chambers to burn, and the dynamite charge explodes in the instant that fire reaches the chamber it is inside.

We would like to light the fuses in some $m$ chambers (at the joints of fuses) in such a way that all the dynamite charges explode in the shortest time possible since the fuses are lit. Write a program that will determine the minimum such time possible.

Input Format

The first line of the standard input holds two integers $n$ and $m$ ($1 ≤ m ≤ n ≤ 300,000$), separated by a single space, that denote, respectively, the number of chambers in the cave and the number of chambers in which fire can be set to the fuses. The chambers are numbered from $1$ to $n$. The next line contains $n$ integers $d_{1},d_{2},…,d_{n}$ ($d_{i} \in \{0,1\}$), separated by single spaces. If $d_{i}=1$, then there is dynamite in the $i$-th chamber, and if $d_{i}=0$, there is none. The following $n-1$ lines specify the corridors of the cave. Each of them holds two integers $a$,$b$ ($1 ≤ a < b ≤ n$), separated by a single space, denoting that there is a corridor connecting the chambers and . Every corridor appears exactly once in the description.

You may assume that in tests worth $10\%$ of the points it holds additionally that $n ≤ 10$, while in tests worth $40\%$ of the points it holds that $n ≤ 1\,000$.

Output Format

The first and only line of the standard output should hold a single integer, equal to the minimum time it takes from lighting the fuses to the explosion of all the charges.

Example

Input

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

Output

1

Notes

For the sample input:

problem_316_1.gif

We light the fuses in chambers 3 and 5. The charge in chamber 3 explodes at time zero, and the charges in chambers 1, 4, 6 and 7 explode after a single time unit.

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.