defmodule Main do use Bitwise, only_operators: true def main do n = IO.read(:line) |> String.trim() |> String.to_integer() total = IO.read(:line) |> String.trim() |> String.to_integer() an = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) {_, ub} = Enum.reduce(0..(n - 1), {an, %{}}, fn i, {[a | an], ub} -> {an, Map.put(ub, i, Enum.reduce(an, a, &(:erlang.max(&1 * &2, &1 + &2))))} end) [a | an] = an an |> Enum.reverse() |> Enum.with_index() |> Enum.reduce(Map.put(%{}, total, [0]), fn {_a, i} = step, dp -> Enum.reduce(dp, %{}, &(&2 |> times(&1, step, ub[i]) |> plus(&1, step, ub[i]))) end) |> Map.get(a) |> Enum.max() |> restore(n) |> Enum.reverse() |> Enum.join("") |> IO.puts() end def times(dp, {rest, wi}, {a, _i}, ub), do: if :math.fmod(rest, a) == 0, do: update(dp, div(rest, a), [Enum.max(wi)], ub), else: dp def plus(dp, {rest, wi}, {a, i}, ub) when a <= rest, do: update(dp, rest - a, Enum.map(wi, fn w -> w + (1 <<< i) end), ub) def plus(dp, {_rest, _wi}, {_a, _i}, _ub), do: dp def update(dp, key, wi, ub) when key <= ub, do: Map.update(dp, key, wi, fn q -> wi ++ q end) def update(dp, _key, _wi, _ub), do: dp def restore(_w, 1), do: [] def restore(w, i), do: [(if (w &&& 1) == 1, do: :+, else: :*) | restore(w >>> 1 ,i - 1)] end