Waterloo 2025 Fall C - Magic Matrix

View as PDF

Submit solution

Points: 12
Time limit: 1.0s
Memory limit: 256M

Problem type

Generative AI models can do many impressive things. They almost seem like magic, even though underneath, they are just large collections of weights—just numbers. Some AI models can even solve programming contest problems, but please don't use them in a contest because that would be cheating.

Your task here is to construct a matrix with some interesting magic properties. You will be given two sequences of numbers of the same length, for example 4 6 5 and 7 8 9. You then need to construct a matrix of integers from 1 to 3 (inclusive) of size 4 + 6 + 5 by 7 + 8 + 9 (so 15 by 24), such that:

  • In exactly 4 of the rows, strictly more than half the integers are 1
  • In exactly 6 of the rows, strictly more than half of the integers are 2
  • In exactly 5 of the rows, strictly more than half of the integers are 3
  • In exactly 7 of the columns, strictly more than half the integers are 1
  • In exactly 8 of the columns, strictly more than half the integers are 2
  • In exactly 9 of the columns, strictly more than half the integers are 3

Hopefully, by now you already see how such a matrix can be constructed, as the task at hand is to do so in the general case.

Input Specification

The first line of input contains an integer k, with 1 \leq k \leq 250, the length of the two given sequences.

The second line contains k integers r_i, with 2 \leq r_i \leq 500, specifying that in exactly r_i rows of the matrix, strictly more than half of the integers should be i. The sum of all the r_i is at most 500.

The third line contains k integers c_i with 2 \leq c_i \leq 500, specifying that in exactly c_i columns of the matrix, strictly more than half of the integers should be i. The sum of all the c_i is at most 500.

Output Specification

If the matrix cannot be formed, output a single line containing the word NO. If the matrix can be formed, output a line containing the word YES, followed by the rows of the matrix, one row per line, with a space separating the integers within a row. If there are multiple possible matrices, output any one of them.

Sample Input 1

2
2 4
3 2

Sample Output 1

YES
2 2 1 1 2
2 1 1 1 1
1 1 2 1 1
2 1 2 1 2
2 2 1 2 2
2 1 1 2 2

Sample Input 2

3
2 2 2
2 2 2

Sample Output 2

YES
3 2 2 1 2 2
3 1 2 1 1 1
2 1 2 2 3 2
1 1 1 1 3 2
3 3 2 1 3 3
3 1 3 3 3 2

Comments