#P15558. [CCPC 2025 哈尔滨站] 扫雪
[CCPC 2025 哈尔滨站] 扫雪
Problem Description
After a heavy snow, little w needs to clean up his yard so that the uneven snow piles look at least a bit neater. The yard can be seen as an grid. The cell in row and column is covered by snow with relative height . Little w has the following operations to deal with the snow piles:
- Choose , and push the snow pile at row , column to the next row. This operation decreases by and increases by . This operation has no cost.
- Choose , and push the snow pile at row , column to the next column. This operation decreases by and increases by . This operation has no cost.
- Choose , and make snow on the pile at row , column . This operation increases by . This operation costs .
- Choose , and remove snow from the pile at row , column . This operation decreases by . This operation costs .
Little w wants to perform some operations to make all equal to , with the minimum total cost. Can you help him compute the minimum cost?
Input Format
The first line contains an integer (), indicating the number of test cases.
Then each test case is given as follows:
The first line contains two integers (), representing the number of rows and columns of the yard.
The next lines describe the grid. Line contains integers (), where the -th integer is the relative snow height in the cell at row , column .
For all testdata, it is guaranteed that the sum of over all test cases does not exceed .
Output Format
For each test case, output one integer per line, representing the minimum cost for little w to reach the goal.
3
1 1
5
1 2
1 -1
2 2
-1 0
1 1
5
0
3
Hint
Translated by ChatGPT 5