This page is still under construction.

Parts of this page are still being built. What you see may change.

Balance

Interview

Time limit1sMemory limit512 MB

Summary
Given an N x N matrix A, find the entrywise-minimal balanced matrix B (satisfying the additive rectangle condition) with B[i][j] >= A[i][j], and report its sum.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Math, Matrix
Solved
No attempts yet

Problem

A matrix AA of size N×NN \times N is called balanced if A[i][j]+A[i+1][j+1]=A[i+1][j]+A[i][j+1]A[i][j] + A[i + 1][j + 1] = A[i + 1][j] + A[i][j + 1] for all 1≤i,j≤N−11 \le i, j \le N - 1.

You are given a matrix AA of size N×NN \times N. Output a matrix BB of the same size such that BB is balanced and B[i][j]≥A[i][j]B[i][j] \ge A[i][j] for all 1≤i,j≤N1 \le i, j \le N. In addition, the sum of the entries of BB must be as small as possible.

Input

The first line contains an integer NN, the number of rows and columns of the matrix (1≤N≤501 \le N \le 50).

Each of the following NN lines contains NN integers. Together they describe the matrix AA. It is guaranteed that 0≤A[i][j]≤35 0000 \le A[i][j] \le 35\,000 for all 1≤i,j≤N1 \le i, j \le N.

Output

On the first line, print the sum of the entries of the balanced matrix BB you found. On the next NN lines, print the balanced matrix in the same format as the input.

Any matrix satisfying the conditions described in the statement is accepted. The values of the output matrix are not constrained in any way (in particular, they may exceed 35 00035\,000).

Examples1

  1. Example 1

    Input
    4
    1 1 1 1
    1 1 1 1
    1 1 1 0
    1 1 1 1
    
    Expected output
    16
    1 1 1 1
    1 1 1 1
    1 1 1 1
    1 1 1 1