eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Numbers Game

Numbers Game

The objective of the numbers game is to use the basic arithmetic operations (\textbf{+}, \textbf{-}, \textbf{*} and \textbf{/}) and a number of given integers (\textbf{4} to \textbf{7} integers) to get as close as possible to a given target integer. Each of the operations can be used multiple times, but each of the integers can be used at-most once. A player wins if he/she manages to calculate the closest possible value to the target integer. For example, the closest possible value to a target of \textbf{20} using the integers \{\textbf{2}, \textbf{3}, \textbf{5}\} can be achieved by the expression \textbf{(2+5)*3}. Your task is to write a program to calculate the answer for each game. Note that "\textbf{/}" stands for integer division (examples are: \textbf{5/4} equals \textbf{1}, \textbf{8/9} equals \textbf{0}.) \InputFile The input consists of many games. The description for each game is given on two lines. The first line contains two integers \textbf{T} and \textbf{N}, separated by a single space, that represent the target integer and the number of given integers.\textbf{-700000} < \textbf{T} < \textbf{700000} and \textbf{4} < \textbf{N} < \textbf{7}. The second line contains N integers separated by single spaces. All integers have values between \textbf{-1000000} and \textbf{1000000} inclusive. Two zeros on a line by themselves, separated by a single space, terminate the input. \OutputFile For each game, the output consists of a single line that contains the closest possible integer to the target. Any answer with the smallest distance to the target is correct.
Ліміт часу 3 секунди
Ліміт використання пам'яті 32 MiB
Вхідні дані #1
30 5
1 2 3 4 5
10000 5
11 2 3 7 5
0 0
Вихідні дані #1
30 30
10000 2310
Джерело The 2011 South Pacific Programming Contest