Blocks on Grid

AtCoder
IDabc186_b
Time2000ms
Memory256MB
Difficulty
We have a grid with $H$ horizontal rows and $W$ vertical columns. The square at the $i$\-th row from the top and $j$\-th column from the left has $A_{i, j}$ blocks stacked on it. At least how many blocks must be removed to make all squares have the same number of blocks? ## Constraints * $1 \leq H,W \leq 100$ * $0\leq A_{i,j} \leq 100$ ## Input Input is given from Standard Input in the following format: $H$ $W$ $A_{1,1}$ $A_{1,2}$ $\ldots$ $A_{1,W}$ $\vdots$ $A_{H,1}$ $A_{H,2}$ $\ldots$ $A_{H,W}$ [samples]
Samples
Input #1
2 3
2 2 3
3 2 2
Output #1
2

Removing $1$ block from the top-right square and $1$ from the bottom-left square makes all squares have $2$ blocks.
Input #2
3 3
99 99 99
99 0 99
99 99 99
Output #2
792
Input #3
3 2
4 4
4 4
4 4
Output #3
0
API Response (JSON)
{
  "problem": {
    "name": "Blocks on Grid",
    "description": {
      "content": "We have a grid with $H$ horizontal rows and $W$ vertical columns. The square at the $i$\\-th row from the top and $j$\\-th column from the left has $A_{i, j}$ blocks stacked on it. At least how many blo",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc186_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "We have a grid with $H$ horizontal rows and $W$ vertical columns. The square at the $i$\\-th row from the top and $j$\\-th column from the left has $A_{i, j}$ blocks stacked on it.\nAt least how many blo...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments