Souvenirs

AtCoder
IDs8pc_3_d
Time2000ms
Memory256MB
Difficulty
Sigma and his brother Sugim are in the $H \\times W$ grid. They wants to buy some souvenirs. Their start position is upper-left cell, and the goal position is lower-right cell. Some cells has a souvenir shop. At $i$-th row and $j$-th column, there is $a_{i, j}$ souvenirs. In one move, they can go left, right, down, and up cell. But they have little time, so they can move only $H+W-2$ times. They wanted to buy souvenirs as many as possible, but they had no computer, so they couldn't get the maximal numbers of souvenirs. Write a program and calculate the maximum souvenirs they can get, and help them. ## Input The input is given from standard input in the following format. > $H \\ W$ $a_{1, 1} \\ a_{1, 2} \\ \\cdots \\ a_{1, W}$ $a_{2, 1} \\ a_{2, 2} \\ \\cdots \\ a_{2, W}$ $\\vdots \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\vdots \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\vdots$ $a_{H, 1} \\ a_{H, 2} \\ \\cdots \\ a_{H, W}$ [samples]
Samples
Input #1
3 3
1 0 5
2 2 3
4 2 4
Output #1
21

The cell at $i$-th row and $j$-th column is denoted $(i, j)$.  
In this case, one of the optimal solution is this:  

*   Sigma moves $(1, 1) -> (1, 2) -> (1, 3) -> (2, 3) -> (3, 3)$.
*   Sugim moves $(1, 1) -> (2, 1) -> (3, 1) -> (3, 2) -> (3, 3)$.

Then, they can get $21$ souvernirs.
Input #2
6 6
1 2 3 4 5 6
8 6 9 1 2 0
3 1 4 1 5 9
2 6 5 3 5 8
1 4 1 4 2 1
2 7 1 8 2 8
Output #2
97
API Response (JSON)
{
  "problem": {
    "name": "Souvenirs",
    "description": {
      "content": "Sigma and his brother Sugim are in the $H \\\\times W$ grid. They wants to buy some souvenirs.   Their start position is upper-left cell, and the goal position is lower-right cell.   Some cells has a so",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "s8pc_3_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Sigma and his brother Sugim are in the $H \\\\times W$ grid. They wants to buy some souvenirs.  \nTheir start position is upper-left cell, and the goal position is lower-right cell.  \nSome cells has a so...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments