結果

問題 No.4 おもりと天秤
ユーザー hotwater2010hotwater2010
提出日時 2020-08-21 23:07:18
言語 Ruby
(3.3.0)
結果
AC  
実行時間 182 ms / 5,000 ms
コード長 661 bytes
コンパイル時間 60 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 19,968 KB
最終ジャッジ日時 2024-04-23 06:30:23
合計ジャッジ時間 3,230 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
12,288 KB
testcase_01 AC 78 ms
12,544 KB
testcase_02 AC 82 ms
13,056 KB
testcase_03 AC 73 ms
12,928 KB
testcase_04 AC 76 ms
13,056 KB
testcase_05 AC 163 ms
19,968 KB
testcase_06 AC 77 ms
12,160 KB
testcase_07 AC 173 ms
19,968 KB
testcase_08 AC 82 ms
19,584 KB
testcase_09 AC 82 ms
19,840 KB
testcase_10 AC 182 ms
19,840 KB
testcase_11 AC 77 ms
12,416 KB
testcase_12 AC 76 ms
12,160 KB
testcase_13 AC 76 ms
12,416 KB
testcase_14 AC 75 ms
12,288 KB
testcase_15 AC 75 ms
12,160 KB
testcase_16 AC 81 ms
12,544 KB
testcase_17 AC 77 ms
12,544 KB
testcase_18 AC 128 ms
19,712 KB
testcase_19 AC 165 ms
19,712 KB
testcase_20 AC 159 ms
19,840 KB
testcase_21 AC 153 ms
19,712 KB
testcase_22 AC 159 ms
19,712 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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]) if $dp[depth+1][total+$w[depth]].nil?
  $dp[depth+1][total] = dfs(depth+1, total) if $dp[depth+1][total].nil?

  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