結果

問題 No.10 +か×か
ユーザー penqen
提出日時 2021-04-05 03:45:31
言語 Elixir
(1.18.1)
結果
AC  
実行時間 2,979 ms / 5,000 ms
コード長 980 bytes
コンパイル時間 4,921 ms
コンパイル使用メモリ 62,248 KB
実行使用メモリ 157,048 KB
最終ジャッジ日時 2024-12-14 15:48:41
合計ジャッジ時間 14,118 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
    warning: missing parentheses for expression following "do:" keyword. Parentheses are required to solve ambiguity inside keywords.

    This error happens when you have function calls without parentheses inside keywords. For example:

        function(arg, one: nested_call a, b, c)
        function(arg, one: if expr, do: :this, else: :that)

    In the examples above, we don't know if the arguments "b" and "c" apply to the function "function" or "nested_call". Or if the keywords "do" and "else" apply to the function "function" or "if". You can solve this by explicitly adding parentheses:

        function(arg, one: if(expr, do: :this, else: :that))
        function(arg, one: nested_call(a, b, c))

    Ambiguity found at:
    │
 27 │    do: if is_nil(dp[key]) || dp[key] < value, do: Map.put(dp, key, value), else: dp
    │    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ Main.exs:27

    warning: use Bitwise is deprecated. import Bitwise instead
    │
  2 │   use Bitwise
    │   ~~~~~~~~~~~
    │
    └─ Main.exs:2: Main (module)

ソースコード

diff #

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
0