結果

問題 No.798 コレクション
コンテスト
ユーザー ikd
提出日時 2018-08-05 00:44:34
言語 Ruby
(4.0.2)
コンパイル:
ruby -w -c _filename_
実行:
ruby _filename_
結果
RE  
実行時間 -
コード長 759 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 50 ms
コンパイル使用メモリ 9,088 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2026-03-17 15:51:29
合計ジャッジ時間 2,202 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 RE * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #
raw source code

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