B - Blocks on Grid Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

H マス、横 W マスのマス目があります。上から i 行目、左から j 列目のマスには、ブロックが A_{i,j} 個あります。

どのマスにも同じ個数のブロックがある状態にするには、最小で何個のブロックを取り除けばよいでしょうか?

制約

  • 1 \leq H,W \leq 100
  • 0\leq A_{i,j} \leq 100

入力

入力は以下の形式で標準入力から与えられる。

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

出力

取り除くブロックの個数の最小値を出力せよ。


入力例 1

2 3
2 2 3
3 2 2

出力例 1

2

右上と左下のマスからそれぞれ 1 つずつブロックを取り除くことで、どのマスにも 2 個のブロックがある状態にできます。


入力例 2

3 3
99 99 99
99 0 99
99 99 99

出力例 2

792

入力例 3

3 2
4 4
4 4
4 4

出力例 3

0

Score : 200 points

Problem Statement

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}

Output

Print the minimum number of blocks that must be removed.


Sample Input 1

2 3
2 2 3
3 2 2

Sample Output 1

2

Removing 1 block from the top-right square and 1 from the bottom-left square makes all squares have 2 blocks.


Sample Input 2

3 3
99 99 99
99 0 99
99 99 99

Sample Output 2

792

Sample Input 3

3 2
4 4
4 4
4 4

Sample Output 3

0