結果

問題 No.527 ナップサック容量問題
ユーザー nebukuro09nebukuro09
提出日時 2017-06-09 22:43:52
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,183 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 120,456 KB
実行使用メモリ 42,400 KB
最終ジャッジ日時 2023-09-03 14:06:45
合計ジャッジ時間 3,488 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 4 ms
6,004 KB
testcase_04 WA -
testcase_05 AC 19 ms
18,564 KB
testcase_06 AC 39 ms
36,560 KB
testcase_07 AC 31 ms
29,684 KB
testcase_08 AC 16 ms
16,872 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 39 ms
36,364 KB
testcase_11 AC 29 ms
26,636 KB
testcase_12 AC 21 ms
20,368 KB
testcase_13 AC 40 ms
37,340 KB
testcase_14 AC 16 ms
16,028 KB
testcase_15 AC 22 ms
22,444 KB
testcase_16 AC 4 ms
5,728 KB
testcase_17 AC 21 ms
18,944 KB
testcase_18 AC 23 ms
21,776 KB
testcase_19 AC 3 ms
4,892 KB
testcase_20 AC 18 ms
17,164 KB
testcase_21 AC 43 ms
42,400 KB
testcase_22 AC 31 ms
29,680 KB
testcase_23 AC 44 ms
40,908 KB
testcase_24 AC 10 ms
11,684 KB
testcase_25 WA -
testcase_26 AC 27 ms
25,052 KB
testcase_27 AC 28 ms
25,612 KB
testcase_28 WA -
testcase_29 AC 44 ms
42,044 KB
testcase_30 AC 6 ms
6,256 KB
testcase_31 AC 21 ms
20,604 KB
testcase_32 AC 27 ms
23,020 KB
testcase_33 AC 23 ms
21,048 KB
testcase_34 AC 27 ms
23,852 KB
testcase_35 AC 25 ms
23,784 KB
testcase_36 WA -
testcase_37 AC 19 ms
18,964 KB
testcase_38 AC 23 ms
22,636 KB
testcase_39 AC 23 ms
21,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

alias Tuple!(int, "v", int, "w") Knap;

void main() {
    auto N = readln.chomp.to!int;
    auto K = new Knap[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!int);
        K[i] = Knap(s[0], s[1]);
    }
    auto V = readln.chomp.to!int;


    K.sort!"a.w < b.w"();

    auto dp = new int[][](N, 10^^5+1);
    dp[0][K[0].w] = K[0].v;

    foreach (i; 1..N) {
        foreach (k; 0..10^^5+1) {
            dp[i][k] = max(dp[i][k], dp[i-1][k]);
            if (k + K[i].w < 10^^5+1)
                dp[i][k+K[i].w] = max(dp[i][k+K[i].w], dp[i-1][k] + K[i].v);
        }
        dp[i][K[i].w] = max(dp[i][K[i].w], K[i].v);
    }

    foreach (j; 0..10^^5) dp[N-1][j+1] = max(dp[N-1][j], dp[N-1][j+1]);

    int minv = 1 << 29;
    int maxv = 1;
    foreach (j; 0..10^^5+1) {
        if (dp[N-1][j] == V) minv = min(minv, j), maxv = max(maxv, j);
    }

    if (K.map!(k => k.v).sum == V) maxv = -1;
    writeln(minv);
    writeln(maxv == -1 ? "inf" : maxv.to!string);
}
0