結果
| 問題 |
No.4 おもりと天秤
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-08-01 15:51:59 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 142 ms / 5,000 ms |
| コード長 | 515 bytes |
| コンパイル時間 | 159 ms |
| コンパイル使用メモリ | 7,424 KB |
| 実行使用メモリ | 15,104 KB |
| 最終ジャッジ日時 | 2024-06-26 09:37:25 |
| 合計ジャッジ時間 | 3,590 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
コンパイルメッセージ
Syntax OK
ソースコード
inputs = STDIN.readlines.map(&:chomp)
N = inputs[0].to_i
Ws = inputs[1].split(/\s+/).map(&:to_i)
total = Ws.inject(:+)
unless total % 2 == 0
puts 'impossible'
exit
end
goal = total / 2
memos = Array.new(N + 1) { Hash.new }
def dp(i, g, memos)
if g == 0
res = true
elsif i == N || g < 0
res = false
elsif memos[i].key?(g)
res = memos[i][g]
else
res = dp(i + 1, g, memos) || dp(i + 1, g - Ws[i], memos)
end
memos[i][g] = res
end
puts dp(0, goal, memos) ? 'possible' : 'impossible'