#CT00000054. Đường qua sân (Crossing the Schoolyard)
Bài tập này chưa được xếp vào lộ trình.
Mục lục đề ⌄
Đường qua sân (Crossing the Schoolyard)
Crossing the Schoolyard
Version: Phuoc Hung OJ Extended
Problem Statement
A schoolyard is divided into a grid of rows and columns. Cell contains an integer , which may be negative, zero, or positive. Rows are numbered from top to bottom, and columns from left to right, starting at .
A student starts at and must reach . Each ordinary move may only be:
- Right: from to .
- Down: from to .
In addition, during the entire route, the student may make at most one diagonal down-right move, from to . A move is valid only if its destination is inside the grid. The diagonal move is optional.
The route's score is the sum of values in the cells actually visited, including the starting and ending cells. When moving diagonally from to , cells and are skipped by that move, so their values are not added. Negative values are counted normally.
Find the maximum possible score of a valid route from to .
Input
- The first line contains two positive integers : the number of rows and columns.
- Each of the next lines contains exactly integers , describing row from left to right.
Output
Print one integer: the maximum total score of any valid route from to . The answer may be negative.
Subtasks
The following general constraints apply to all subtasks: ; ; .
- Subtask 1 (30%): .
- Subtask 2 (30%): or .
- Subtask 3 (40%): No additional restrictions beyond the general constraints.
Examples
Example 1
Input
3 3
2 -100 5
4 8 -100
3 -100 10
Output
24
Explanation
One maximum-scoring route is . The first three cells have values ; the final move is diagonal to the cell valued .
The two cells and , both valued , are skipped by that diagonal move. The score is , so the answer is .
Example 2
Input
2 3
1 2 3
4 5 6
Output
16
Explanation
A route with no diagonal move is , scoring . In this grid, no route with a diagonal move scores more than either. Therefore the answer is .
Example 3
Input
1 1
-7
Output
-7
Explanation
The grid consists of one cell, , whose value is . It is both the starting and ending cell, and the score is , not .
Liên quan
Trong các cuộc thi sau: