cho.sh
Notes
Loading...

Number Board Construction

Time limit

5s

Memory limit

128 MB

Problem

There is an N by N number board. Each cell must contain a nonnegative integer. The sum of each row and the sum of each column are given in advance, and the board must be filled so that all of those sums are satisfied.

For instance, when N=2, the required row and column sums may be shown as follows.

row sum
??12
??4
610

The numbers to the right are row sums, and the numbers below are column sums. There can be many boards that satisfy the same sums. Three possible boards for the situation above are:

57
13
66
04
48
22

Among all valid boards, choose one that minimizes the largest value written in any cell. In the situation above, a board whose largest value is 6 is optimal. Write a program that constructs such a board.

Input

The first line contains N, the number of rows and columns (1 ≤ N ≤ 50).

The second line contains N integers: the required sums of rows 1 through N, in order.

The third line contains N integers: the required sums of columns 1 through N, in order.

Each given sum is between 0 and 10000, inclusive. The input is guaranteed to allow at least one valid board.

Output

On the first line, print the largest value used in your board.

On each of the next N lines, print one row of the board. Every printed value must be a nonnegative integer, all row sums and column sums must match the input, and the largest printed value must be as small as possible.