#P12976. 受力分析 Force
受力分析 Force
Background
Comentropy, who was still writing problems, suddenly received a challenge—surprisingly, it was a mechanics problem. He was shocked, but once he saw the Constraints, he immediately knew how to solve it. However, the process was long and tedious, so he asked you to help simplify it.
Note: This problem has no strict physical theory behind it, and it does not require any physics background.
It is recommended to read the formal statement.
Problem Description
There are cubic blocks arranged in a square grid. Each block has its own weight, and you need to keep them balanced and stationary. This requires that for the block in row and column , the supporting force on its bottom face must lie within the interval .
The problem setters provide steel wires in the horizontal direction and steel wires in the vertical direction to provide the supporting force, and place the blocks at the wire intersection points. Now you can apply forces to these wires separately. (Note that the force is the same everywhere along the same wire.)
Definition: Numerically, the supporting force received by a block equals the sum of the forces you apply to the two wires beneath it.

As shown in the figure, one set of wires is placed horizontally, with applied forces ; the other set is placed vertically, with applied forces . The resultant force at intersection is .
Please find the force applied to each wire. To make it harder for Comentropy, the setters also require the sequence to be lexicographically smallest. Can you help him handle this problem?
Each force must be a non-negative integer, pointing upward.
Formally: Given two matrices , find non-negative integer sequences and such that . Among all solutions, output the one that makes the combined sequence lexicographically smallest.
Input Format
The first line contains a positive integer , indicating an matrix.
Lines to contain a matrix representing the lower bounds .
Lines to contain another matrix representing the upper bounds .
Output Format
If there is no solution, output
-1
directly.
Otherwise:
The first line contains numbers, representing the forces on the horizontal wires .
The second line contains numbers, representing the forces on the vertical wires .
Note: If there are multiple answers, output the lexicographically smallest one.
2
2 4
1 5
3 5
2 7
0 0
2 5
Hint
Sample 1 Explanation: There may be another solution with , but is a lexicographically smaller solution, and it can be proven to be lexicographically smallest.
For of the testdata, it is guaranteed that .
For of the testdata, it is guaranteed that , .
For of the testdata, it is guaranteed that .
For the remaining of the testdata, it is guaranteed that .
For of the testdata, it is guaranteed that , .
Please pay attention to the impact of constants on the running efficiency of your program.
Translated by ChatGPT 5