Numbers and Letters

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.

Problem information

Author: Salvador Roura
Event: Vint-i-quatrè Concurs de Programació de la UPC - Semifinal
Date: 2026-06-18

Generation: 2026-06-10T19:44:06.672Z

© Jutge.org, 2006–2026.
https://jutge.org