If You Are Homeless... Just Build a House (Розбір)
For a given region h×w with values b_1\le b_2\le \ldots \le b_{hw}, the answer is the sum of ∣bi−bmed∣, where med = \lfloor \frac{hw}{2} \rfloor. It can also be stated as the following: let's keep the set of smallest med numbers $S_1