P7239 3D Cube

Background

**The testdata for this problem is completely handmade, and has been manually verified.** idea & data: [zrmpaul](/user/226760) tester: [Hilarious_Reality](/user/80049) -------- Little Z likes 3D models and observes them carefully from different angles.

Description

Little Z got a 2D matrix. The base can be viewed as an $n \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 $0$. For example, `12321` has one "peak", while `13231` has two "peaks". More examples: |Cube arrangement|Number of "peaks"| |:----:|:----:| |$\texttt{123}$|$1$| |$\texttt{212}$|$2$| |$\texttt{122221}$|$1$| |$\texttt{00011000}$|$1$| |$\texttt{10010101}$|$4$| 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, m$. The second line contains $n$ integers: the left view. The $i$-th integer represents the number of cubes in the $i$-th column of the left view. The third line contains $m$ integers: the front view. The $j$-th integer represents the number of cubes in the $j$-th column of the front view. The next $n$ lines each contain $m$ 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 $n$ lines, each containing $m$ non-negative integers, representing the constructed top view of the 3D matrix. The integer in row $i$ and column $j$ 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.**

Explanation/Hint

### Constraints **All subtasks in this problem use bundled tests.** For $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\%$ of the testdata: $n \times m \leq 25$. In the top view, each cell has at most $7\times10^8$ cubes, and the number of `1` in the top view is $\leq 20$. Translated by ChatGPT 5