結果
| 問題 | No.4 おもりと天秤 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-08-01 15:51:59 |
| 言語 | Ruby (4.0.2) |
| 結果 |
AC
|
| 実行時間 | 81 ms / 5,000 ms |
| + 70µs | |
| コード長 | 515 bytes |
| 記録 | |
| コンパイル時間 | 118 ms |
| コンパイル使用メモリ | 9,308 KB |
| 実行使用メモリ | 17,664 KB |
| 最終ジャッジ日時 | 2026-07-30 11:29:28 |
| 合計ジャッジ時間 | 2,897 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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'