#ABC227F. [ABC227F] Treasure Hunting
[ABC227F] Treasure Hunting
Score : points
Problem Statement
We have a grid with horizontal rows and vertical columns. Let denote the square at the -th row from the top and -th column from the left. contains an integer .
Takahashi will depart and repeatedly move one square right or down until reaching . It is not allowed to exit the grid.
The cost of this travel is defined as:
the sum of the greatest integers among the integers written on the squares traversed.
Find the minimum possible cost.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
There is only one way to travel, where the traversed squares contain the integers , , from largest to smallest, so we print .
The minimum cost is achieved by traversing , , in this order.