#C526. Maximum Treasure Collection
Maximum Treasure Collection
Maximum Treasure Collection
You are given a grid with n rows and m columns. Each cell in the grid contains a certain amount of treasure. Starting from the top-left corner of the grid, your task is to determine the maximum amount of treasure one can collect by moving only to the right or down until reaching the bottom-right corner of the grid.
The transition can be formulated in a dynamic programming fashion as follows:
\( dp[i][j] = \max(dp[i-1][j], dp[i][j-1]) + grid[i][j] \)
where \(dp[i][j]\) represents the maximum collectible treasure when reaching cell \((i, j)\). Note that the indices are based on 0-indexing.
inputFormat
The first line contains two integers n and m representing the number of rows and columns of the grid respectively.
This is followed by n lines, each containing m space-separated integers representing the treasure in each cell of the grid.
Example:
3 3 1 3 1 1 5 1 4 2 1
outputFormat
Output a single integer representing the maximum treasure that can be collected from the top-left to the bottom-right cell.
## sample3 3
1 3 1
1 5 1
4 2 1
12