AT_abc393_g [ABC393G] Unevenness

Description

There is a grid with $ N $ rows and $ N $ columns. Let $ (i,j) $ denote the cell at the $ i $ -th row and $ j $ -th column. $ (i,j) $ contains an integer $ A_{i,j} $ . You are also given two coprime positive integers $ P $ and $ Q $ . You may perform the following operation any number of times (possibly zero), as long as the total cost does not exceed $ \displaystyle \frac{P}{Q} $ : - Choose a positive real number $ x $ . Choose one cell in the grid and either increase or decrease the value in that cell by $ x $ . This operation incurs a cost of $ x $ . After performing all operations, let $ B_{i,j} $ be the value in cell $ (i,j) $ . Define the **non-uniformity** $ U $ as the sum of absolute differences of adjacent cells. Formally, $ \displaystyle U = \sum_{i=1}^N \sum_{j=1}^{N-1} \vert B_{i,j} - B_{i,j+1} \vert + \sum_{i=1}^{N-1} \sum_{j=1}^N \vert B_{i,j} - B_{i+1,j} \vert $ . Find the minimum possible value of $ U $ after performing the operations in an optimal way, and print that value. Also, print one valid final configuration $ B_{i,j} $ that achieves this minimum $ U $ .

Input Format

The input is given from Standard Input in the following format: > $ N $ $ P $ $ Q $ $ A_{1,1} $ $ A_{1,2} $ $ \dots $ $ A_{1,N} $ $ A_{2,1} $ $ A_{2,2} $ $ \dots $ $ A_{2,N} $ $ \vdots $ $ A_{N,1} $ $ A_{N,2} $ $ \dots $ $ A_{N,N} $

Output Format

Print $ U $ and $ B_{i,j} $ in the following format: > $ U $ $ B_{1,1} $ $ B_{1,2} $ $ \dots $ $ B_{1,N} $ $ B_{2,1} $ $ B_{2,2} $ $ \dots $ $ B_{2,N} $ $ \vdots $ $ B_{N,1} $ $ B_{N,2} $ $ \dots $ $ B_{N,N} $ Your output is considered correct if it meets all of the following conditions. (Note that the tolerance for $ U $ is very strict.) - The absolute or relative error between your output $ U $ and its true minimum value is at most $ 2^{-51} (\approx 4.44 \times 10^{-16}) $ . - The absolute or relative error between $ \sum_{i=1}^N \sum_{j=1}^{N-1} \vert B_{i,j} - B_{i,j+1} \vert + \sum_{i=1}^{N-1} \sum_{j=1}^N \vert B_{i,j} - B_{i+1,j} \vert $ and $ U $ is at most $ 10^{-10} $ . - $ \sum_{i=1}^N \sum_{j=1}^N \vert A_{i,j} - B_{i,j}\vert $ is at most $ \displaystyle \frac{P}{Q} + \max \left(1, \frac{P}{Q} \right) \times 10^{-10} $ . If there are multiple solutions, you may print any one of them.

Explanation/Hint

### Sample Explanation 1 By performing the following operations, we obtain $ U = 24 $ , which is the minimum possible value. The total cost is $ 2 + 1 = 3 $ . - Let $ x = 2 $ . Decrease the value in cell $ (1,2) $ by $ 2 $ . - Let $ x = 1 $ . Increase the value in cell $ (2,1) $ by $ 1 $ . ### Constraints - $ 2 \leq N \leq 10 $ - $ 1 \leq P \leq 10^{12} $ - $ 1 \leq Q \leq 10^{12} $ - $ \gcd(P, Q) = 1 $ - $ 0 \leq A_{i,j} \leq 10 $ - All input values are integers.