AtCoder Tour

AtCoder
IDabc358_g
Time2000ms
Memory256MB
Difficulty
AtCoder Land is represented by a grid with $H$ rows and $W$ columns. Let $(i, j)$ denote the cell at the $i$\-th row and $j$\-th column. Takahashi starts at cell $(S_i, S_j)$ and repeats the following action $K$ times: * He either stays in the current cell or moves to an adjacent cell. After this action, if he is in cell $(i, j)$, he gains a fun value of $A_{i, j}$. Find the maximum total fun value he can gain. Here, a cell $(x', y')$ is considered adjacent to cell $(x, y)$ if and only if $|x - x'| + |y - y'| = 1$. ## Constraints * $1 \leq H, W \leq 50$ * $1 \leq K \leq 10^9$ * $1 \leq S_i \leq H$ * $1 \leq S_j \leq W$ * $1 \leq A_{i, j} \leq 10^9$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $H$ $W$ $K$ $S_i$ $S_j$ $A_{1, 1}$ $A_{1, 2}$ $\ldots$ $A_{1, W}$ $A_{2, 1}$ $A_{2, 2}$ $\ldots$ $A_{2, W}$ $\vdots$ $A_{H, 1}$ $A_{H, 2}$ $\ldots$ $A_{H, W}$ [samples]
Samples
Input #1
2 3 3
1 2
2 1 2
3 4 5
Output #1
14

Takahashi can gain a total fun value of $14$ by acting as follows:

*   Initially, he is at $(1, 2)$.
*   He moves to cell $(2, 2)$. Then, he gains a fun value of $A_{2, 2} = 4$.
*   He moves to cell $(2, 3)$. Then, he gains a fun value of $A_{2, 3} = 5$.
*   He stays in cell $(2, 3)$. Then, he gains a fun value of $A_{2, 3} = 5$.

He cannot gain a total fun value greater than $14$, so print $14$.
Input #2
2 2 1000000000
2 1
100 100
100 99
Output #2
100000000000
API Response (JSON)
{
  "problem": {
    "name": "AtCoder Tour",
    "description": {
      "content": "AtCoder Land is represented by a grid with $H$ rows and $W$ columns. Let $(i, j)$ denote the cell at the $i$\\-th row and $j$\\-th column. Takahashi starts at cell $(S_i, S_j)$ and repeats the following",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc358_g"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "AtCoder Land is represented by a grid with $H$ rows and $W$ columns. Let $(i, j)$ denote the cell at the $i$\\-th row and $j$\\-th column.\nTakahashi starts at cell $(S_i, S_j)$ and repeats the following...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments