結果

問題 No.4 おもりと天秤
ユーザー penqenpenqen
提出日時 2021-03-14 02:50:59
言語 Elixir
(1.16.2)
結果
AC  
実行時間 725 ms / 5,000 ms
コード長 1,014 bytes
コンパイル時間 1,035 ms
コンパイル使用メモリ 56,228 KB
実行使用メモリ 71,280 KB
最終ジャッジ日時 2023-08-30 01:18:01
合計ジャッジ時間 16,499 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 552 ms
49,260 KB
testcase_01 AC 557 ms
49,212 KB
testcase_02 AC 556 ms
49,420 KB
testcase_03 AC 555 ms
50,624 KB
testcase_04 AC 549 ms
49,276 KB
testcase_05 AC 638 ms
56,784 KB
testcase_06 AC 547 ms
49,908 KB
testcase_07 AC 639 ms
56,344 KB
testcase_08 AC 552 ms
49,392 KB
testcase_09 AC 725 ms
71,280 KB
testcase_10 AC 644 ms
57,236 KB
testcase_11 AC 548 ms
49,388 KB
testcase_12 AC 550 ms
49,236 KB
testcase_13 AC 544 ms
49,224 KB
testcase_14 AC 548 ms
49,512 KB
testcase_15 AC 552 ms
49,624 KB
testcase_16 AC 557 ms
49,416 KB
testcase_17 AC 551 ms
49,836 KB
testcase_18 AC 630 ms
57,748 KB
testcase_19 AC 628 ms
56,376 KB
testcase_20 AC 624 ms
55,428 KB
testcase_21 AC 627 ms
57,216 KB
testcase_22 AC 627 ms
58,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

defmodule Main do
  def main do
    n = IO.read(:line) |> String.trim() |> String.to_integer()
    wn = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
    solve(n, wn) |> IO.puts()
  end

  def solve(n, wn) do
    with sum <- Enum.sum(wn),
      0 <- rem(sum, 2) do
        max = round(sum / 2)
        dp = %{ 0 => 0..max |> Enum.reduce(%{}, &Map.put(&2, &1, 0))}
        wn
        |> Enum.with_index()
        |> Enum.reduce(dp, fn {w, i}, dp ->
          dp_i = 0..max
          |> Enum.reduce(dp[i], fn j, acc ->
            dp_j = if w <= j do
              max(dp[i][j - w] + w, dp[i][j])
            else
              dp[i][j]
            end
            Map.put(acc, j, dp_j)
          end)
          Map.put(dp, i + 1, dp_i)
        end)
        |> (fn dp ->
          dp[n][max]
        end).()
        |> Kernel.==(max)
        |> if do
          :possible
        else
          :impossible
        end
    else
      _ ->
        :impossible
    end
  end
end
0