A harvest cart rolls across a vineyard laid out as a grid of R rows and C columns. Each cell holds an integer yield: positive for ripe vines, negative for rotted patches that cost yield when passed through. The cart starts at the top-left cell (whose yield is always collected) and must reach the bottom-right cell, moving on each step to the cell immediately to the right or immediately below. Every cell the cart enters contributes its yield to the running total.
Determine the maximum total yield the cart can collect.
Line 1: two integers R and C.
Next R lines: each contains C integers, the yields of that row.
A single integer: the maximum total yield of a right/down route from the top-left cell to the bottom-right cell (this value may be negative).
Example 1
Input
2 2 1 2 3 4
Expected
8
Explanation
The two routes collect 1+2+4 = 7 and 1+3+4 = 8; the maximum is 8.
Example 2
Input
1 4 -1 -2 3 -1
Expected
-1
Explanation
A single row forces the cart through every cell, giving -1 + -2 + 3 + -1 = -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →