結果

問題 No.4 おもりと天秤
ユーザー letrangerjpletrangerjp
提出日時 2017-05-14 11:20:24
言語 Ruby
(3.3.0)
結果
AC  
実行時間 300 ms / 5,000 ms
コード長 422 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 11,328 KB
実行使用メモリ 23,204 KB
最終ジャッジ日時 2023-09-08 17:27:42
合計ジャッジ時間 4,298 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,116 KB
testcase_01 AC 80 ms
15,244 KB
testcase_02 AC 84 ms
15,228 KB
testcase_03 AC 79 ms
15,276 KB
testcase_04 AC 79 ms
15,048 KB
testcase_05 AC 166 ms
17,584 KB
testcase_06 AC 78 ms
15,172 KB
testcase_07 AC 177 ms
17,556 KB
testcase_08 AC 80 ms
15,248 KB
testcase_09 AC 84 ms
15,116 KB
testcase_10 AC 188 ms
19,064 KB
testcase_11 AC 78 ms
15,112 KB
testcase_12 AC 80 ms
15,144 KB
testcase_13 AC 80 ms
15,160 KB
testcase_14 AC 81 ms
15,016 KB
testcase_15 AC 80 ms
15,020 KB
testcase_16 AC 81 ms
15,236 KB
testcase_17 AC 79 ms
15,088 KB
testcase_18 AC 300 ms
23,204 KB
testcase_19 AC 160 ms
17,308 KB
testcase_20 AC 146 ms
17,016 KB
testcase_21 AC 147 ms
16,996 KB
testcase_22 AC 145 ms
17,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def f(n, weights)
  sum = weights.inject(0, :+)
  return false if sum.odd?

  dp = {}
  g = lambda{|i, w|
    return true if w == 0
    return false if i >= n || w < 0
    # nilは偽なのでhash||=じゃダメ
    return dp[[i,w]] if !dp[[i,w]].nil?

    dp[[i,w]] = g.(i+1, w) || g.(i+1, w-weights[i])
  }
  g.(0, sum/2)
end

N = gets.to_i
W = gets.split.take(N).map(&:to_i).sort
puts f(N, W) ? :possible : :impossible
0