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 \times M matrices L and R, and matrices A and B of dimensions N \times 2 and M \times 2 respectively, all of the following conditions must hold:

  • L_{ij} \le K_{ij} \le R_{ij} for all (1 \le i \le N, 1 \le j \le M)
  • A_{i1} \le \sum_{j=1}^{M} K_{ij} \le A_{i2} for all (1 \le i \le N)
  • B_{j1} \le \sum_{i=1}^{N} K_{ij} \le B_{j2} for all (1 \le j \le M)

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

Constraints

1 \le N, M \le 1000

0 \le L_{ij} \le R_{ij} \le 10^6

0 \le A_{i1} \le A_{i2} \le 10^9

0 \le B_{j1} \le B_{j2} \le 10^9

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

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

Sample Output

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.