結果

問題 No.4 おもりと天秤
ユーザー d_nishiyama85
提出日時 2016-06-13 00:24:35
言語 Ruby
(3.4.1)
結果
AC  
実行時間 196 ms / 5,000 ms
コード長 555 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2024-06-26 09:34:26
合計ジャッジ時間 3,744 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'pp'

$ws = []
$n = 0
$dp = {}

def main
  $n = gets.to_i
  $ws = gets.strip.split(' ').map{|e| e.to_i}
  $n.times{|i| $dp[i] = {}}
  sum = $ws.reduce(:+)
  result = solve(0, 0, sum)
  puts result ? "possible" : "impossible"
end

def solve(i, acc, sum)
  if (i === $n)
    return false
  end
  if $dp[i].key?(acc)
    return $dp[i][acc]
  end
  if (sum % 2 === 1)
    return false
  end
  if (acc * 2 === sum)
    return true
  end
  nex = $ws[i]
  result = solve(i + 1, acc + nex, sum) || solve(i + 1, acc, sum)
  $dp[i][acc] = result
end

main()
0