結果

問題 No.4 おもりと天秤
ユーザー vjudge1
提出日時 2025-09-04 16:44:18
言語 Crystal
(1.14.0)
結果
RE  
実行時間 -
コード長 532 bytes
コンパイル時間 15,534 ms
コンパイル使用メモリ 309,660 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-04 16:44:35
合計ジャッジ時間 14,133 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

n = gets.not_nil!.to_i
w = Array(Int32).new(n) { gets.not_nil!.to_i }

t = w.sum
if t.odd?
  puts "impossible"
  exit
end

th = t // 2

# Create a 2D array with n+1 rows and th+1 columns, initialized to 0
m = Array(Array(Int32)).new(n + 1) { Array(Int32).new(th + 1, 0) }

(1...n).each do |i|
  (0..th).each do |j|
    if j < w[i]
      m[i][j] = m[i - 1][j]
    else
      m[i][j] = Math.max(m[i - 1][j - w[i]] + w[i], m[i - 1][j])
    end

    if m[i][j] == th
      puts "possible"
      exit
    end
  end
end

puts "impossible"
0