#K78722. Matrix Traversal Under Sum Constraint
Matrix Traversal Under Sum Constraint
Matrix Traversal Under Sum Constraint
You are given a matrix of positive integers with dimensions \(N\) rows and \(M\) columns, and a constraint \(S\). Your task is to determine whether it is possible to traverse the matrix from the top-left corner (cell \((1,1)\)) to the bottom-right corner (cell \((N,M)\)) such that the sum of the numbers along the path is at most \(S\). You may only move either to the right or downward at any step.
Note: The path sum includes the values of the starting and ending cells.
For instance, consider a matrix:
1 3 1 1 5 1 4 2 1
If \(S = 15\), a valid minimal path from the top-left to bottom-right is \(1 \rightarrow 3 \rightarrow 1 \rightarrow 1 \rightarrow 1\) with a sum of 7, hence the answer is "YES".
inputFormat
The input is read from standard input and consists of:
- A line containing three integers \(N\), \(M\), and \(S\), where \(N\) and \(M\) represent the number of rows and columns of the matrix, and \(S\) is the maximum allowed sum.
- Next, there are \(N\) lines, each containing \(M\) space-separated integers, representing the rows of the matrix.
outputFormat
Output a single line to standard output containing either "YES" if there exists a path from the top-left to the bottom-right corner with a sum less than or equal to \(S\), or "NO" if no such path exists.
## sample3 3 15
1 3 1
1 5 1
4 2 1
YES