Under construction

Still being built. Things may change

Advanced Eggplant Farm

Time limit3sMemory limit1024 MB

Summary
Given a grid of eggplant sizes, assign non-negative integer heights so that each adjacent pair respects the size order, with the minimum total height.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Graph, Topological sort
Solved
No attempts yet

Problem

Mingu owns an advanced eggplant farm. The farm is a grid of NN rows and MM columns, and an eggplant is planted in each of the N×MN \times M soil cells. Mingu noticed that eggplants that receive more water grow faster. To even out the sizes, he wants to give more water to the smaller eggplants.

To do this, the height of each soil cell can be changed to any non-negative integer. Water flows from higher ground to lower ground, so more water collects in the lower of two adjacent cells. If two adjacent cells have the same height, the same amount of water collects in each. Two cells are adjacent if they share a side.

To even out the sizes, for every pair of adjacent cells, the soil under the larger eggplant must be higher than the soil under the smaller one. If two adjacent eggplants have the same size, their soil heights must be equal.

Currently, every soil cell has height 00. Changing the height of one cell to hh costs hh units of work, where hh is a non-negative integer. Find the height arrangement that evens out the sizes and has the minimum total work.

In other words, you are given a matrix SS of size N×MN \times M of positive integers, where Si,jS_{i,j} is the size of the eggplant in row ii, column jj. Find a matrix HH of size N×MN \times M of non-negative integers, where Hi,jH_{i,j} is the height of the soil in row ii, column jj, that satisfies the following conditions.

  • For all integers i,j,k,li, j, k, l with 1≤i,k≤N1 \le i, k \le N, 1≤j,l≤M1 \le j, l \le M, and ∣i−k∣+∣j−l∣=1|i-k| + |j-l| = 1:
    • If Si,j<Sk,lS_{i,j} < S_{k,l}, then Hi,j<Hk,lH_{i,j} < H_{k,l}.
    • If Si,j=Sk,lS_{i,j} = S_{k,l}, then Hi,j=Hk,lH_{i,j} = H_{k,l}.
    • If Si,j>Sk,lS_{i,j} > S_{k,l}, then Hi,j>Hk,lH_{i,j} > H_{k,l}.
  • The sum of all elements of HH is minimal.

Input

The first line contains the number of rows NN and the number of columns MM, separated by a space. (1≤N,M≤1 0001 \le N, M \le 1\,000)

Each of the next NN lines contains MM positive integers separated by spaces, which are the eggplant sizes in that row. The jj-th number on the ii-th line is the eggplant size Si,jS_{i,j} in row ii, column jj. (1≤Si,j≤1091 \le S_{i,j} \le 10^9)

Output

Print NN lines, each containing MM non-negative integers separated by spaces.

The jj-th number on the ii-th line is Hi,jH_{i,j}, the soil height in row ii, column jj, in the arrangement with the minimum total work.

The matrix HH that answers the problem is unique.

Examples3

  1. Example 1

    Input
    2 3
    1 5 2
    2 5 1
    
    Expected output
    0 2 1
    1 2 0
    
  2. Example 2

    Input
    3 4
    6 7 4 10
    5 4 9 7
    10 9 1 2
    
    Expected output
    2 3 0 3
    1 0 3 2
    2 1 0 1
    
  3. Example 3

    Input
    5 5
    4 3 3 3 3
    4 2 2 1 1
    2 2 2 5 5
    2 2 5 5 5
    2 2 5 5 5
    
    Expected output
    3 2 2 2 2
    3 1 1 0 0
    1 1 1 2 2
    1 1 2 2 2
    1 1 2 2 2