LogiQuest

387. Root-to-Leaf Path Sum

EasyDFS

Print YES if some root-to-leaf path has values summing to target, else NO.

Input format

Line 1: n target Line 2: the values in level order, -1 for a missing child

Output format

YES or NO

Example 1

Input

13 22
5 4 8 11 -1 13 4 7 2 -1 -1 -1 1

Output

YES

Example 2

Input

3 5
1 2 3

Output

NO

Constraints

1 ≤ nodes ≤ 5000 |values| < 1000 (no node has value -1)

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…