結果

問題 No.288 貯金箱の仕事
ユーザー 👑 hos.lyric
提出日時 2015-02-12 22:12:41
言語 Ruby
(3.4.1)
結果
TLE  
実行時間 -
コード長 802 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-06-23 19:20:05
合計ジャッジ時間 7,684 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27 TLE * 1 -- * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:50: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

#!/usr/bin/env ruby

INF = 10**18 + 1;

n, m = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
ks = gets.split.map(&:to_i)

lim = as[-1]
dp = []
loop do
  dp = (0 .. lim).map{ INF }
  dp[0] = 0
  as.each do |a|
    a.upto(lim) do |x|
      if dp[x] > dp[x - a] + 1
        dp[x] = dp[x - a] + 1
      end
    end
  end
  if lim >= as[-1] then
    ok = true
    lim.downto(lim - as[-1]) do |x|
      ok &&= (dp[x] == INF || dp[x] == dp[x - as[-1]] + 1)
    end
    if ok then
      break
    end
  end
  lim <<= 1
end

STDERR.puts lim

target = -m
n.times do |i|
  target += as[i] * ks[i];
end

if target < 0
  ans = INF
elsif target < lim
  ans = dp[target]
else
  greed = ((target - lim) + as[-1] - 1) / as[-1]
  ans = greed + dp[target - as[-1] * greed]
end

if ans >= INF
  p -1
else
  p ans
end
0