結果

問題 No.4 おもりと天秤
ユーザー letrangerjpletrangerjp
提出日時 2017-05-14 11:35:31
言語 Ruby
(3.3.0)
結果
AC  
実行時間 474 ms / 5,000 ms
コード長 493 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 11,580 KB
実行使用メモリ 25,748 KB
最終ジャッジ日時 2023-09-08 17:28:02
合計ジャッジ時間 5,825 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
15,260 KB
testcase_01 AC 79 ms
15,052 KB
testcase_02 AC 83 ms
15,192 KB
testcase_03 AC 81 ms
15,208 KB
testcase_04 AC 84 ms
15,092 KB
testcase_05 AC 438 ms
24,232 KB
testcase_06 AC 79 ms
15,112 KB
testcase_07 AC 470 ms
25,128 KB
testcase_08 AC 80 ms
15,368 KB
testcase_09 AC 85 ms
15,224 KB
testcase_10 AC 474 ms
25,748 KB
testcase_11 AC 81 ms
15,112 KB
testcase_12 AC 82 ms
15,264 KB
testcase_13 AC 80 ms
15,072 KB
testcase_14 AC 81 ms
15,028 KB
testcase_15 AC 81 ms
15,268 KB
testcase_16 AC 80 ms
15,360 KB
testcase_17 AC 79 ms
15,168 KB
testcase_18 AC 414 ms
23,972 KB
testcase_19 AC 410 ms
23,692 KB
testcase_20 AC 456 ms
25,608 KB
testcase_21 AC 424 ms
23,768 KB
testcase_22 AC 413 ms
23,848 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def f(n, weights)
  sum = weights.inject(0, :+)
  return false if sum.odd?

  dp = {}
  g = lambda{|i, w|
    return dp[[i,w]] if !dp[[i,w]].nil?

    dp[[i,w]] = if w == 0
      true
    elsif i >= n || w < 0
      false # falseを代入するから、hash未定義を真偽で判定する書き方は注意
    else
      g.(i+1, w) || g.(i+1, w-weights[i])
    end
  }
  g.(0, sum/2)
end

N = gets.to_i
W = gets.split.take(N).map(&:to_i).sort.reverse
puts f(N, W) ? :possible : :impossible
0