defmodule Main do use Bitwise 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) solve(n, total, an) |> IO.puts() end def solve(n, total, an) do [a | an] = an an |> Enum.reduce(Map.put(%{}, a, 0), fn a, dp -> Enum.reduce(dp, %{}, fn {acc, wi}, ndp -> ndp |> put(acc + a, (wi <<< 1) + 1, total) |> put(acc * a, (wi <<< 1), total) end) end) |> (fn dp -> dp[total] |> restore(n) |> Enum.reverse() |> Enum.join("") end).() end def restore(_w, 1), do: [] def restore(w, i), do: [(if (w &&& 1) == 1, do: :+, else: :*) | restore(w >>> 1 ,i - 1)] def put(dp, key, value, total) when key <= total, do: if is_nil(dp[key]) || dp[key] < value, do: Map.put(dp, key, value), else: dp def put(dp, _key, _value, _total), do: dp end