#K45897. Maximum Crops Collection
Maximum Crops Collection
Maximum Crops Collection
You are given an n x m grid where each cell (i, j) contains a certain number of crops. Starting in the top-left corner (0, 0) and moving only right or down, your task is to collect the maximum number of crops by the time you reach the bottom-right corner (n-1, m-1).
The state transition formula is given by: $$dp[i][j] = \max(dp[i-1][j],\ dp[i][j-1]) + grid[i][j]$$ where the boundary conditions are that you can only move right in the first row and down in the first column.
Calculate and output the maximum number of crops that can be collected.
inputFormat
The input consists of:
- The first line contains two space-separated integers n and m — the number of rows and columns in the grid.
- The next n lines each contain m space-separated integers representing the number of crops available in each cell of the grid.
outputFormat
Output a single integer representing the maximum number of crops that can be collected from the top-left to the bottom-right corner following the allowed moves.
## sample3 3
1 3 1
1 5 1
4 2 1
12