結果

問題 No.798 コレクション
ユーザー ikdikd
提出日時 2018-08-05 00:44:34
言語 Ruby
(3.2.2)
結果
RE  
実行時間 -
コード長 759 bytes
コンパイル時間 61 ms
コンパイル使用メモリ 11,348 KB
実行使用メモリ 15,564 KB
最終ジャッジ日時 2023-09-11 02:23:36
合計ジャッジ時間 3,503 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
15,016 KB
testcase_01 AC 84 ms
15,148 KB
testcase_02 AC 85 ms
15,056 KB
testcase_03 AC 83 ms
15,148 KB
testcase_04 AC 83 ms
15,152 KB
testcase_05 AC 83 ms
15,168 KB
testcase_06 AC 83 ms
15,092 KB
testcase_07 AC 84 ms
15,152 KB
testcase_08 AC 85 ms
15,076 KB
testcase_09 AC 90 ms
14,984 KB
testcase_10 AC 92 ms
15,092 KB
testcase_11 AC 86 ms
15,060 KB
testcase_12 AC 86 ms
15,164 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 85 ms
15,084 KB
testcase_24 RE -
testcase_25 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.chomp.to_i
a, b = [], []
n.times do
  _a, _b = gets.split.map(&:to_i)
  a.push(_a); b.push(_b)
end

inf = 1_000_000_000
dp = Array.new(1 << n) { inf }
dp[0] = 0
(1 << n).times do |state|
  next if dp[state] == inf
  cnt = state.to_s(2).count("1")
  day = cnt - cnt / 3
  n.times do |i|
    next if (state & (1 << i)) > 0
    s, nexstate = dp[state] + a[i] + b[i] * day, state ^ (1 << i)
    if day % 2 == 1
      if nexstate == ((1 << n) - 1)
        dp[nexstate] = s if s < dp[nexstate]
      else
        n.times do |j|
          next if (nexstate & (1 << j)) > 0
          dp[nexstate ^ (1 << j)] = s if s < dp[nexstate ^ (1 << j)]
        end
      end
    else
      dp[nexstate] = s if s < dp[nexstate]
    end
  end
end

puts dp[(1 << n) - 1]
0