結果

問題 No.798 コレクション
ユーザー ikd
提出日時 2018-08-05 00:44:34
言語 Ruby
(3.4.1)
結果
RE  
実行時間 -
コード長 759 bytes
コンパイル時間 47 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-06-28 16:53:35
合計ジャッジ時間 3,536 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 RE * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
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