結果

問題 No.4 おもりと天秤
ユーザー Naoki_M_
提出日時 2016-09-07 09:39:58
言語 Ruby
(3.4.1)
結果
AC  
実行時間 198 ms / 5,000 ms
コード長 440 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-06-26 09:59:32
合計ジャッジ時間 3,722 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.chomp.to_i
ws = gets.chomp.split.map(&:to_i)

possible = Array.new(2){Array.new(10001, false)}
possible[0][0] = true

s = 0
(0...n).to_a.each do |i|
  w = ws[i]
  for j in 0..s
    possible[(i + 1) % 2][j] = false
  end
  for j in 0..s
    if possible[i % 2][j]
      possible[(i + 1) % 2][j + w] = true
      possible[(i + 1) % 2][(j - w).abs] = true
    end
  end
  s += w
end

puts possible[n % 2][0] ? 'possible' : 'impossible'
0