LogiQuest

305. Search a Sorted Matrix

MediumStaircase Search

Each row is sorted left to right and each column top to bottom. Print YES if x is present, else NO.

Input format

Line 1: r c Next r lines: c integers Last line: x

Output format

YES or NO

Example 1

Input

5 5
1 4 7 11 15
2 5 8 12 19
3 6 9 16 22
10 13 14 17 24
18 21 23 26 30
5

Output

YES

Example 2

Input

5 5
1 4 7 11 15
2 5 8 12 19
3 6 9 16 22
10 13 14 17 24
18 21 23 26 30
20

Output

NO

Constraints

1 ≤ r, c ≤ 1000

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

Loading the MyLoginn IDE…