Numbers and Letters

Consider the following rules for a game, similar to those of a famous TV
show: Six numbers x₁, …, x₆ are generated at random between 1 and 1000.
A random number g from 1 to 10⁶ is also generated. The goal is to get
the number g, 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 x_(i) may be used more than once.
Additionally, all intermediate results must be between 0 and 10⁹. The
divisions must be exact. Obviously, we cannot divide by zero.

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

Input

Input consists of several cases, each with g and x₁, …, x₆.

Output

For every case, print the result that is closest to g. 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
