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() [a | an] = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) an |> Enum.reverse() |> Enum.with_index(0) |> Enum.reduce(Map.put(%{}, total, [0]), fn {a, i}, dp -> Enum.reduce(dp, %{}, fn {rest, wi}, ndp -> ndp = if :math.fmod(rest, a) == 0, do: Map.update(ndp, div(rest, a), wi, fn q -> wi ++ q end), else: ndp if a <= rest do wi = Enum.map(wi, fn w -> w + (1 <<< i) end) Map.update(ndp, rest - a, wi, fn q -> wi ++ q end) else ndp end end) end) |> Map.get(a) |> Enum.max() |> restore(n) |> Enum.reverse() |> Enum.join("") |> IO.puts() end def restore(_w, 1), do: [] def restore(w, i), do: [(if (w &&& 1) == 1, do: :+, else: :*) | restore(w >>> 1 ,i - 1)] end