結果

問題 No.527 ナップサック容量問題
ユーザー te-shte-sh
提出日時 2018-01-26 15:46:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,060 bytes
コンパイル時間 627 ms
コンパイル使用メモリ 84,888 KB
実行使用メモリ 22,956 KB
最終ジャッジ日時 2023-09-03 18:23:44
合計ジャッジ時間 3,309 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 7 ms
5,920 KB
testcase_06 AC 23 ms
18,260 KB
testcase_07 AC 15 ms
12,820 KB
testcase_08 AC 5 ms
4,808 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 23 ms
17,536 KB
testcase_11 AC 10 ms
8,992 KB
testcase_12 AC 7 ms
6,188 KB
testcase_13 AC 25 ms
19,384 KB
testcase_14 AC 4 ms
5,120 KB
testcase_15 AC 11 ms
8,760 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 7 ms
6,128 KB
testcase_18 AC 8 ms
6,976 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 5 ms
5,928 KB
testcase_21 AC 27 ms
22,560 KB
testcase_22 AC 15 ms
12,684 KB
testcase_23 AC 22 ms
19,620 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 13 ms
10,808 KB
testcase_26 AC 11 ms
9,280 KB
testcase_27 AC 12 ms
10,816 KB
testcase_28 AC 10 ms
9,500 KB
testcase_29 AC 28 ms
22,956 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 11 ms
9,548 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 7 ms
6,112 KB
testcase_38 AC 10 ms
8,792 KB
testcase_39 AC 8 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
T[] readArray(T)(size_t n){auto a=new T[](n),r=readln.splitter;foreach(ref v;a){v=r.front.to!T;r.popFront;}return a;}
T[] readArrayM(T)(size_t n){auto a=new T[](n);foreach(ref v;a)v=readln.chomp.to!T;return a;}

void main()
{
  int n; readV(n);
  auto v = new int[](n), w = new int[](n);
  foreach (i; 0..n) readV(v[i], w[i]);
  int vs; readV(vs);

  auto ws = w.sum;
  auto dp = new int[][](n+1, ws+1);
  foreach (i; 0..n+1) dp[i][] = -1;
  dp[0][0] = 0;

  foreach (i; 0..n)
    foreach (j; 0..ws+1) {
      dp[i+1][j] = dp[i][j];
      if (j >= w[i] && dp[i][j-w[i]] >= 0)
        dp[i+1][j] = max(dp[i+1][j], dp[i][j-w[i]]+v[i]);
    }

  auto m1 = false, m2 = false;
  foreach (i; 0..ws+1) {
    if (dp[n][i] == vs && !m1) {
      m1 = true;
      writeln(max(i, 1));
    }
    if (dp[n][i] > vs && !m2) {
      m2 = true;
      writeln(i-1);
    }
  }

  if (!m2) writeln("inf");
}
0