結果

問題 No.4 おもりと天秤
ユーザー hacchahaccha
提出日時 2023-07-02 19:29:03
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 586 bytes
コンパイル時間 440 ms
コンパイル使用メモリ 11,476 KB
実行使用メモリ 19,776 KB
最終ジャッジ日時 2023-09-23 15:26:43
合計ジャッジ時間 7,825 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,156 KB
testcase_01 AC 74 ms
15,352 KB
testcase_02 AC 79 ms
15,248 KB
testcase_03 AC 74 ms
15,240 KB
testcase_04 AC 83 ms
15,284 KB
testcase_05 AC 79 ms
15,244 KB
testcase_06 AC 76 ms
15,076 KB
testcase_07 AC 75 ms
15,104 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def recursive(left, right, remain, weights)
  if remain == 0
    return left == right
  end

  if (left - right).abs > remain
    return false
  end

  w = weights.shift

  result_left = recursive(left + w, right, remain - w, weights)
  if result_left
    weights.unshift(w)
    return result_left
  end

  result_right = recursive(left, right + w, remain - w, weights)
  weights.unshift(w)
  return result_right
end

N = gets.to_i
weights = gets.split.map(&:to_i)

remain = weights.sum
result = recursive(0, 0, remain, weights)

if result
  puts "possible"
else
  puts "impossible"
end
0