#P7011. Escape the Dungeon Alive

    ID: 20218 Type: Default 1000ms 256MiB

Escape the Dungeon Alive

Escape the Dungeon Alive

After an epic fight with the emperor lich, the hero finds himself in a dungeon consisting of ( n ) chambers and ( n-1 ) corridors connecting them. The hero starts at chamber 1 with ( 0 ) hit-points (HP) and must reach a target chamber ( t ). Each chamber, upon the first visit, either reduces the hero's HP (if a monster is present) or increases his HP (if there is a magic pool). The HP effect of chamber ( i ) is given by an integer ( a_i ) (which can be negative or positive). Moving along the corridors, the hero can choose the order in which he visits chambers (even revisiting chambers without triggering additional effects) in order to accumulate HP before encountering unavoidable negative effects. Note that if at any point the hero's HP drops below zero, his journey ends in tragedy.

Determine whether there exists an order of first visits such that the hero can move from chamber 1 to chamber ( t ) while ensuring his HP never falls below 0.

Input Format:

  • The first line contains two integers \( n \) and \( t \) (the number of chambers and the target chamber number, respectively).
  • The second line contains \( n \) integers \( a_1, a_2, \dots, a_n \) where \( a_i \) represents the HP effect in chamber \( i \) (positive for magic pools, negative for monsters).
  • The next \( n-1 \) lines each contain two integers \( u \) and \( v \), representing a corridor connecting chambers \( u \) and \( v \).

Output Format:

  • Output a single line: Yes if the hero can reach chamber \( t \) alive (i.e. his HP never drops below 0 during the journey), and No otherwise.

Note: The hero can visit chambers in any order as long as each corridor is used to travel between chambers that have been reached. The effect in each chamber is applied only on the first visit.

inputFormat

Input:

  • The first line contains two integers ( n ) and ( t ).
  • The second line contains ( n ) integers, where the ( i )-th integer represents the HP effect ( a_i ) in chamber ( i ).
  • The following ( n-1 ) lines each contain two integers ( u ) and ( v ), indicating that there is a corridor connecting chambers ( u ) and ( v ). It is guaranteed that all chambers are reachable from chamber 1.

outputFormat

Output:

  • A single line containing either Yes or No indicating whether the hero can escape the dungeon alive.

sample

5 5
0 2 -3 1 5
1 2
1 3
2 4
4 5
Yes