Playing with numbers P44029


Statement
 

pdf   zip

thehtml

Given n numbers, compute all the different results that can be obtained with the sum and product operators and adding parentheses at will. You cannot change the order of the given numbers. For instance, with 2, 1 and 3 we can get 5, 6, 8 or 9, but no other result. Some possible combinations are (2 · 1) + 3 = 5, 2 · (1 · 3) = 6, 2 · (1 + 3) = 8, i (2 + 1) · 3 = 9.

Input

Input consists of several cases, each with n, followed by n natural numbers between 1 and ‍9. Assume 1 ≤ n ≤ 9.

Output

For every case, print all the possible results in order.

Public test cases
  • Input

    3  2 1 3
    2  1 1
    4  2 5 8 3
    4  9 9 9 9
    

    Output

    5 6 8 9
    1 2
    18 21 29 31 32 34 41 45 54 57 58 59 77 78 83 86 110 122 126 168 240
    36 99 162 171 243 324 738 810 1458 6561
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Salvador Roura
    Original language
    Catalan
    Other languages
    Catalan Spanish
    Official solutions
    C++
    User solutions
    C++