Singularity Cup P5 - Perfect Matrix

View as PDF

Submit solution

Points: 20
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Kevin wants a "perfect matrix" with N rows and M columns.

He has certain expectations for some matrix K that he considers perfect.

He has a desired integer range for each element of K, as well as a desired integer range for each row sum and column sum.

More formally, given 2 other N×M matrices L and R, and matrices A and B of dimensions N×2 and M×2 respectively, all of the following conditions must hold:

  • LijKijRij for all (1iN,1jM)
  • Ai1j=1MKijAi2 for all (1iN)
  • Bj1i=1NKijBj2 for all (1jM)

Help Kevin find any perfect matrix or determine that it is impossible to do so.

Constraints

1N,M1000

0LijRij106

0Ai1Ai2109

0Bj1Bj2109

Input Specification

The first line of input contains integers N and M.

The next N lines of input each contain M space-separated integers representing the matrix L, the minimum values of each element.

The next N lines of input each contain M space-separated integers representing the matrix R, the maximum values of each element.

The next N lines of input each contain 2 space-separated integers representing the matrix A, the minimum and maximum sums of each row.

The next M lines of input each contain 2 space-separated integers representing the matrix B, the minimum and maximum sums of each column.

Output Specification

Output N lines of M space-separated integers representing any perfect matrix that satisfies Kevin or report that it is impossible to do so by outputting -1.

Sample Input

Copy
2 2
3 0
0 3
3 9
9 3
5 5
1 6
6 9
0 9

Sample Output

Copy
3 2
3 3

Explanation for Sample

In the example above, this is the only matrix that Kevin will consider perfect.


Comments

There are no comments at the moment.