A harbor uses two parallel beacon lines, the Upper Line and the Lower Line, strung along n numbered positions. At every position, the beacon on each line is either lit (1) or unlit (0). A dispatcher recorded three facts about last night's run: the total number of beacons lit on the Upper Line, the total number lit on the Lower Line, and, for every single position, the combined number of lit beacons at that position summed across both lines (which can only be 0, 1, or 2). Your job is to reconstruct one beacon pattern for both lines that is consistent with all of these recorded facts, or to report that no such pattern could have produced them.
Line 1: three integers n upper lower — the number of positions, the required total lit count on the Upper Line, and the required total lit count on the Lower Line.
Line 2: n integers colSum[1] colSum[2] ... colSum[n], the combined lit count at each position (each is 0, 1, or 2).
If no valid pair of lines can satisfy all three recorded facts, print a single line containing -1.
Otherwise print exactly two lines, each with n space-separated integers (each 0 or 1): the Upper Line's pattern, then the Lower Line's pattern. Any pattern satisfying every constraint is accepted.
Example 1
Input
3 2 1 1 1 1
Expected
1 1 0 0 0 1
Explanation
No position needs both lines lit, so every position needs exactly one of the two lines lit. The Upper Line needs 2 lit beacons total and the Lower Line needs 1, so lighting the Upper Line at positions 1 and 2 and the Lower Line at position 3 gives rows "1 1 0" and "0 0 1": each column sums to 1, the Upper Line has 2 lit beacons, and the Lower Line has 1.
Example 2
Input
2 0 0 1 2
Expected
-1
Explanation
Position 2 requires a combined count of 2, meaning both lines must be lit there. But upper = 0 and lower = 0 mean neither line is allowed any lit beacon at all, which contradicts that requirement, so no valid pattern exists and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →