結果

問題 No.288 貯金箱の仕事
ユーザー te-sh
提出日時 2017-06-08 15:54:39
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,002 ms / 2,000 ms
コード長 774 bytes
コンパイル時間 1,581 ms
コンパイル使用メモリ 151,808 KB
実行使用メモリ 493,988 KB
最終ジャッジ日時 2024-06-12 19:49:15
合計ジャッジ時間 15,062 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

const inf = 10 ^^ 9;

void main()
{
  auto rd = readln.split, n = rd[0].to!size_t, m = rd[1].to!long;
  auto ai = readln.split.to!(int[]);
  auto ki = readln.split.to!(long[]);

  auto b = zip(ai, ki).map!"a[0] * a[1]".sum;
  if (b < m) {
    writeln(-1);
    return;
  }

  auto c = b - m;
  auto r = max((c - ai[$-1] ^^ 2) / ai[$-1], 0);
  c -= r * ai[$-1];

  auto dp = new int[][](n+1, c+1);
  foreach (ref dpi; dp) dpi[] = inf;
  dp[0][0] = 0;

  foreach (i; 0..n) {
    foreach (j; 0..c+1) {
      dp[i+1][j] = dp[i][j];
      if (j >= ai[i])
        dp[i+1][j] = min(dp[i+1][j], dp[i+1][j-ai[i]] + 1);
    }
  }

  auto s = dp[$-1][$-1];
  if (s == inf)
    writeln(-1);
  else
    writeln(r + s);
}
0