QOJ.ac

QOJ

実行時間制限: 1.5 s メモリ制限: 1024 MB 満点: 100

#6658. Dungeon

統計

Chul-soo and Young-hee are playing a game and have to pass through a dungeon. The dungeon is a grid of size $N \times N$. The rows of the grid are numbered from $0$ to $N-1$ from top to bottom, and the columns are numbered from $0$ to $N-1$ from left to right. The cell located at row $i$ and column $j$ is called cell $(i, j)$.

The rules for passing through the dungeon are as follows:

  • Chul-soo starts at the top-left cell of the dungeon and moves to the bottom-right cell. When moving, Chul-soo can only move to an immediately adjacent cell to the right or down from his current position.
  • Young-hee starts at the top-right cell of the dungeon and moves to the bottom-left cell. When moving, Young-hee can only move to an immediately adjacent cell to the left or down from her current position.

Each cell in the dungeon contains one item. The value of each item is a positive integer, zero, or a negative integer, and the value of the item in cell $(i, j)$ is $V[i][j]$. Chul-soo and Young-hee already know the values of the items in all cells. After passing through the dungeon, they collect all the items in the cells they have visited. Note that if both people pass through the same cell, they only collect the item in that cell once.

Write a program to find the maximum possible sum of the values of the items collected by Chul-soo and Young-hee.

Implementation Details

You must implement the following function:

int max_item_sum(vector<vector<int> > V)
  • $V$: A 2D integer array of size $N \times N$. For all $i, j$ ($0 \le i, j \le N-1$), $V[i][j]$ is the value of the item in cell $(i, j)$ of the dungeon.
  • This function should return the maximum possible sum of the values of the items that Chul-soo and Young-hee can collect.

You must not execute any input/output functions in any part of your submitted source code.

Constraints

  • $2 \le N \le 1000$
  • $-100\,000 \le V[i][j] \le 100\,000$ for all $i, j$ ($0 \le i, j \le N-1$)

Subtasks

  1. (11 points) $N \le 5$
  2. (44 points) $N \le 300$
  3. (15 points) $V[i][j] \ge 0$ for all $i, j$ ($0 \le i, j \le N-1$)
  4. (30 points) No additional constraints.

Examples

Input 1

N = 5
V = [[-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

-9

Note

Figure 1. The dungeon grid for Example 1.

The blue cells on the left in the figure indicate the cells visited by Chul-soo, and the yellow cells on the right indicate the cells visited by Young-hee. The sum of the values of the items in the cells visited only by Chul-soo is $-4$, the sum of the values of the items in the cells visited only by Young-hee is also $-4$, and the sum of the values of the items in the cells visited by both is $-1$. Therefore, the total sum of the values of the items is $-9$, and this is the case where the sum of the values is maximized.

Input 2

N = 5
V = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2], [4, 5, 1, 2, 3], [5, 1, 2, 3, 4]]

Output 2

60

Input 3

N = 5
V = [[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 3

15

Note

The sample grader receives input in the following format:

  • Line 1: $N$
  • Line $2 + i$ ($0 \le i \le N - 1$): $V[i][0] \ V[i][1] \ \dots \ V[i][N - 1]$

The sample grader outputs the following:

  • Line 1: The value returned by the function max_item_sum

Please note that the sample grader may differ from the grader used in actual grading.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.