Numbers and Letters P56003


Statement
 

pdf   zip

Consider the following rules for a game, similar to those of a famous TV show: Six numbers x1,,x6x_1, \dots, x_6 are generated at random between 1 and 1000. A random number gg from 1 to 10610^6 is also generated. The goal is to get the number gg, or as close to it as possible. To do so, we may add, substract, multiply and divide the numbers of any non-empty subset of the six given numbers. No xix_i may be used more than once. Additionally, all intermediate results must be between 0 and 10910^9. The divisions must be exact. Obviously, we cannot divide by zero.

Can you compute the result that is closest to gg? For instance, if g=982g = 982 and we have the numbers {100,75,50,25,6,3}\{ 100, 75, 50, 25, 6, 3 \}, a possible solution (exact, in this case) is 982=6((100+75)3)50.982 = 6 \cdot ((100 + 75) - 3) - 50 \enspace .

Input

Input consists of several cases, each with gg and x1,,x6x_1, \dots, x_6.

Output

For every case, print the result that is closest to gg. If there is a tie, choose the largest result.

Public test cases
  • Input

    982  100 75 50 25 6 3
    1000  10 10 10 10 10 10
    1003  10 10 10 10 10 10
    1  213 769 552 695 207 999
    1000000  1 1 1 1 1 1
    42  1000 42 867 999 600 235
    

    Output

    982
    1000
    1002
    3
    9
    42
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++