QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100

#4109. Exploration Route

Estadísticas

The jungle you are facing can be represented as an $n \times m$ grid, where the cell at row $i$ and column $j$ represents an area with an integer weight $v(i, j)$ (which may be negative), indicating the profit or cost of visiting that area. Each cell can be visited at most once, and you cannot move outside the boundaries of the map. You are required to start at the first row and first column and end at the $n$-th row and $m$-th column. Your goal is to maximize the sum of the weights of all visited cells.

For some reason, your expedition route is subject to certain restrictions. Initially, you are at the starting point. In each day's action, you must first choose one of the four directions (up, down, left, or right) and move $0$ steps (i.e., stay put) or any number of steps along that direction. Then, you must choose a direction again (which can be the same as or different from the previous direction) and continue moving along that direction until you reach a boundary of the map, which concludes that day's expedition. The expedition can last for any number of days. The boundary position where one day's expedition ends becomes the starting position for the next day, unless that day is the end of the entire expedition. Note that because each cell can be visited only once, and your final destination must be the cell at row $n$ and column $m$, you need to plan each day's route carefully. You want to know the maximum possible profit of the entire expedition journey, i.e., the maximum sum of weights you can obtain.

Input

The first line contains two integers, representing the total number of rows $n$ and columns $m$.

Following this are $n$ lines, each containing $m$ integers. The integer at the $i$-th row and $j$-th column corresponds to the profit or cost of visiting the area at row $i$ and column $j$.

Output

Output a single integer representing the sum of the weights of all visited cells in the optimal expedition route.

Examples

Input 1

10 10
+1 +1 +1 +1 +1 -1 -1 -1 -1 -1
-1 -1 +1 +1 +1 -1 -1 -1 -1 -1
-1 -1 +1 +1 +1 +1 +1 +1 +1 +1
+1 +1 +1 +1 -1 -1 -1 -1 -1 +1
+1 -1 -1 +1 -1 -1 -1 -1 -1 +1
+1 +1 +1 +1 -1 -1 -1 -1 -1 +1
+1 +1 +1 +1 +1 +1 +1 +1 +1 +1
+1 -1 -1 -1 -1 -1 -1 -1 -1 -1
+1 +1 +1 +1 -1 +1 +1 +1 +1 +1
-1 -1 -1 +1 +1 +1 -1 -1 -1 +1

Output 1

53

Subtasks

For $10\%$ of the data, $n, m \leq 5$.

For $40\%$ of the data, $n, m \leq 40$.

For $70\%$ of the data, $n, m \leq 100$.

For all data, $5 \leq n, m \leq 800$, and the absolute value of the area profit or cost is within $100000$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.