#C7342. Maximum Bananas Collection
Maximum Bananas Collection
Maximum Bananas Collection
You are given a grid with m rows and n columns. Each cell of the grid contains an integer that represents the number of bananas available at that cell. A monkey starts at the top-left corner of the grid (cell (0, 0)) and aims to reach the bottom-right corner (cell (m-1, n-1)). The monkey can only move either to the right or downward at any step.
Your task is to determine the maximum number of bananas the monkey can collect along a valid path from the start to the destination.
The transition equation for dynamic programming is given by:
$$dp[i][j] = \max(dp[i-1][j],\ dp[i][j-1]) + grid[i][j] $$with the initial conditions:
$$dp[0][0] = grid[0][0], \quad dp[0][j] = dp[0][j-1] + grid[0][j], \quad dp[i][0] = dp[i-1][0] + grid[i][0]. $$inputFormat
The first line contains two integers m and n, representing the number of rows and columns in the grid, respectively.
This is followed by m lines, each containing n space-separated integers that represent the grid values.
It is guaranteed that m, n > 0.
outputFormat
Output a single integer which is the maximum number of bananas the monkey can collect from the top-left to the bottom-right corner of the grid.
## sample3 3
0 1 0
1 0 1
0 1 0
2