Pillars P85783


Statement
 

pdf   zip

The world-famous architect Mr. Fruí from Reus is planning to build a colossal pillar HH units high. Mr. Fruí has nn black pieces with heights b1,,bnb_1, \dots, b_n, and mm white pieces with heights w1,,wmw_1, \dots, w_m. According to his design, the pillar must have four pieces: a black piece at its bottom, a white piece above it, another black piece above, and finally a white piece at the top of the pillar.

Mr. Fruí wishes to know which combination of four pieces with total height HH is the most stable. Given two combinations A=[a1,a2,a3,a4]A = [a_1, a_2, a_3, a_4] and B=[b1,b2,b3,b4]B = [b_1, b_2, b_3, b_4] (where a1a_1 denotes the height of the bottom (black) piece of the pillar AA, a2a_2 denotes the height of the second (white) piece of AA, and so on), we say that AA is more stable than BB if a1>b1a_1 > b_1, or if a1=b1a_1 = b_1 but a2>b2a_2 > b_2, etc. In other words, AA is more stable than BB if and only if the sequence of heights of the pieces of AA is lexicographically larger than the sequence of heights of the pieces of BB.

Write a program such that, given the desired height HH of the pillar, the heights of the black pieces and the heights of the white pieces, computes which pillar (if any) of height exactly HH would be the most stable.

Input

Input consists of several cases, each in three lines. The first line has HH, an integer number between 1 and 41084 \cdot 10^8. The second and third lines consist respectively of b1,,bnb_1, \dots, b_n and of w1,,wmw_1, \dots, w_m. A blank line separates two cases. Assume 2n10002 \le n \le 1000 and 2m10002 \le m \le 1000, and that no piece has a height larger than 10810^8.

Output

For every case, print the sequence of heights of the pieces of the most stable pillar, from bottom to top. If no solution exists, print “no solution”.

Public test cases
  • Input

    100
    20 20
    30 10 30 50
    
    100
    20 10 4
    50 30 45
    

    Output

    20 50 20 10
    no solution
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++