#K52137. Max Treasure Collection

    ID: 29243 Type: Default 1000ms 256MiB

Max Treasure Collection

Max Treasure Collection

In this problem, you are given a grid of non-negative integers representing an island where each cell contains a certain amount of treasure. Your task is to find the maximum amount of treasure that can be collected starting from the top-left corner and reaching the bottom-right corner, moving only to the right or down at each step.

Formally, let the grid be represented as a matrix (A) of dimensions (n \times m). You are to compute the maximum sum along a path from (A_{1,1}) to (A_{n,m}) subject to the moves: right (\to) and down (\downarrow).

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two integers \(n\) and \(m\) separated by a space, representing the number of rows and columns.
  • The next \(n\) lines each contain \(m\) non-negative integers separated by spaces, representing the grid.

outputFormat

Output a single integer to standard output (stdout), which is the maximum treasure that can be collected by following the allowed movements.## sample

3 3
1 3 1
1 5 1
4 2 1
12