#P7239. 3D Cube

3D Cube

Description

Little Z got a 2D matrix. The base can be viewed as an n×mn \times m grid.

On each cell, you may stack any number of cubes. Due to gravity, cubes will not float in the air, and they are not glued together.

He provides the three orthographic views of this 3D matrix. Please construct a cube arrangement that satisfies the following condition:

For each row and each column, there is at most one "peak". In particular, the heights around the border of the base matrix are considered to be 00. For example, 12321 has one "peak", while 13231 has two "peaks".

More examples:

Cube arrangement Number of "peaks"
123\texttt{123} 11
212\texttt{212} 22
122221\texttt{122221} 11
00011000\texttt{00011000}
10010101\texttt{10010101} 44

Find the matrix that uses the minimum number of cubes. If there is no solution, output -1.

Input Format

The first line contains two integers: n,mn, m.

The second line contains nn integers: the left view. The ii-th integer represents the number of cubes in the ii-th column of the left view.

The third line contains mm integers: the front view. The jj-th integer represents the number of cubes in the jj-th column of the front view.

The next nn lines each contain mm integers: the top view, where 0 means there is no cube, and 1 means there is at least one cube.

Output Format

If there is no solution, output -1.

Otherwise output nn lines, each containing mm non-negative integers, representing the constructed top view of the 3D matrix. The integer in row ii and column jj denotes the number of cubes at that position.

Make sure to output exactly in the required format, otherwise the SPJ may return results such as UKE.

3 3
2 2 2
2 2 2
0 1 0
1 1 1
0 1 0
0 2 0 
2 2 2 
0 2 0 

Hint

Constraints

All subtasks in this problem use bundled tests.

For 20%20\% of the testdata: the input files are given, and your program should output the answers by table lookup; see the attachment easy.zip.

For 100%100\% of the testdata: n×m25n \times m \leq 25. In the top view, each cell has at most 7×1087\times10^8 cubes, and the number of 1 in the top view is 20\leq 20.

Translated by ChatGPT 5