結果

問題 No.4 おもりと天秤
ユーザー hotwater2010
提出日時 2020-08-22 00:27:58
言語 Ruby
(3.4.1)
結果
AC  
実行時間 190 ms / 5,000 ms
コード長 599 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 19,840 KB
最終ジャッジ日時 2024-10-15 06:34:37
合計ジャッジ時間 3,873 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

$n = gets.to_i
$w = gets.split.map(&:to_i)
$nerai = $w.inject(:+)/2.0

$dp = Array.new($n){Array.new(100*100+1, nil)}
def dfs(depth, total)
  if depth >= $n - 1
    return total + $w[depth] if total + $w[depth] == $nerai
    return total if total == $nerai
    return -1
  end

  $dp[depth+1][total+$w[depth]] ||= dfs(depth+1, total+$w[depth])
  $dp[depth+1][total] ||= dfs(depth+1, total)

  return $dp[depth+1][total+$w[depth]] if $nerai == $dp[depth+1][total+$w[depth]]
  return $dp[depth+1][total] if $nerai == $dp[depth+1][total]
  return -1
end

puts dfs(-1, 0) > 0 ? "possible" : "impossible"
0