結果
問題 |
No.4 おもりと天秤
|
ユーザー |
|
提出日時 | 2017-02-11 12:03:48 |
言語 | Ruby (3.4.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 550 bytes |
コンパイル時間 | 264 ms |
コンパイル使用メモリ | 7,552 KB |
実行使用メモリ | 12,544 KB |
最終ジャッジ日時 | 2024-12-29 12:08:39 |
合計ジャッジ時間 | 3,377 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | RE * 23 |
コンパイルメッセージ
Main.rb:21: warning: assigned but unused variable - half Main.rb:2: warning: assigned but unused variable - w_list Main.rb:3: warning: assigned but unused variable - half Main.rb:4: warning: assigned but unused variable - dp Syntax OK
ソースコード
N = gets.to_i w_list = gets.chomp.split.map(&:to_i) half = 0 dp = Array.new(100, Array.new(10001, true)) def solve(idx, total) return false if idx >= N || total > half return dp[idx][total] if dp[idx][total] == false return true if total == half return true if solve(idx + 1, total) || solve(idx + 1, total + w_list[idx]) return dp[idx][total] = false end def main total = w_list.inject(:+) return "impossible" if total & 1 == 1 half = total >> 1 w_list.sort! return solve(0, 0) ? "possible" : "impossible" end puts main()