結果

問題 No.4 おもりと天秤
ユーザー nakamura sosukenakamura sosuke
提出日時 2019-07-18 18:59:09
言語 Ruby
(3.3.0)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 393 bytes
コンパイル時間 37 ms
コンパイル使用メモリ 11,196 KB
実行使用メモリ 25,792 KB
最終ジャッジ日時 2023-08-26 20:31:31
合計ジャッジ時間 3,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,300 KB
testcase_01 AC 81 ms
15,232 KB
testcase_02 AC 77 ms
15,116 KB
testcase_03 AC 78 ms
15,116 KB
testcase_04 AC 77 ms
15,276 KB
testcase_05 AC 115 ms
24,576 KB
testcase_06 AC 81 ms
15,296 KB
testcase_07 AC 114 ms
24,188 KB
testcase_08 AC 76 ms
15,056 KB
testcase_09 AC 78 ms
15,552 KB
testcase_10 AC 125 ms
25,792 KB
testcase_11 AC 76 ms
15,284 KB
testcase_12 AC 77 ms
15,248 KB
testcase_13 AC 77 ms
15,104 KB
testcase_14 AC 77 ms
15,260 KB
testcase_15 AC 76 ms
15,016 KB
testcase_16 AC 77 ms
15,044 KB
testcase_17 AC 77 ms
15,244 KB
testcase_18 AC 93 ms
19,264 KB
testcase_19 AC 109 ms
22,800 KB
testcase_20 AC 107 ms
22,236 KB
testcase_21 AC 107 ms
22,024 KB
testcase_22 AC 107 ms
22,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
W = gets.split.map(&:to_i).sort!

def f(n, ws)
  sum = 0; ws.each{ |w| sum += w }
  #puts "  sum=#{sum}->#{sum/2}"
  return false if sum.odd?

  dp = {}
  dp[0] = true

  ws.each do | w |
    dpc = dp.clone
    dpc.keys.each do | d |
      dp[d+w] = dpc[d]
    end
  end
  #dp.sort.each{|k,v| puts "k=#{k}, v=#{v}"}
  return dp[sum/2]
end
puts f(N, W) ? 'possible' : 'impossible'
0