結果

問題 No.527 ナップサック容量問題
ユーザー tentententen
提出日時 2020-12-01 20:03:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 1,424 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 74,468 KB
実行使用メモリ 108,152 KB
最終ジャッジ日時 2023-10-11 03:42:32
合計ジャッジ時間 13,590 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
58,284 KB
testcase_01 AC 146 ms
55,896 KB
testcase_02 AC 146 ms
56,036 KB
testcase_03 AC 146 ms
57,852 KB
testcase_04 AC 132 ms
56,032 KB
testcase_05 AC 221 ms
70,672 KB
testcase_06 AC 332 ms
86,748 KB
testcase_07 AC 292 ms
84,848 KB
testcase_08 AC 207 ms
66,928 KB
testcase_09 AC 132 ms
55,852 KB
testcase_10 AC 337 ms
88,980 KB
testcase_11 AC 269 ms
77,304 KB
testcase_12 AC 231 ms
77,428 KB
testcase_13 AC 339 ms
89,032 KB
testcase_14 AC 203 ms
66,916 KB
testcase_15 AC 250 ms
77,656 KB
testcase_16 AC 145 ms
57,856 KB
testcase_17 AC 220 ms
70,588 KB
testcase_18 AC 236 ms
78,252 KB
testcase_19 AC 145 ms
58,200 KB
testcase_20 AC 197 ms
66,928 KB
testcase_21 AC 367 ms
107,776 KB
testcase_22 AC 285 ms
84,908 KB
testcase_23 AC 357 ms
108,064 KB
testcase_24 AC 172 ms
66,716 KB
testcase_25 AC 284 ms
84,832 KB
testcase_26 AC 260 ms
77,560 KB
testcase_27 AC 270 ms
77,548 KB
testcase_28 AC 247 ms
77,696 KB
testcase_29 AC 359 ms
108,152 KB
testcase_30 AC 154 ms
60,668 KB
testcase_31 AC 224 ms
70,968 KB
testcase_32 AC 251 ms
77,172 KB
testcase_33 AC 230 ms
77,656 KB
testcase_34 AC 250 ms
77,568 KB
testcase_35 AC 256 ms
77,724 KB
testcase_36 AC 145 ms
58,008 KB
testcase_37 AC 222 ms
70,544 KB
testcase_38 AC 243 ms
77,336 KB
testcase_39 AC 236 ms
77,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int[] values;
    static int[] weights;
    static int[][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        values = new int[n];
        weights = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            weights[i] = sc.nextInt();
        }
        dp = new int[n][100001];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        int v = sc.nextInt();
        int min = 0;
        int max = 0;
        for (int i = 1; i <= 100000; i++) {
            int x = dfw(n - 1, i);
            if (x == v) {
                if (min == 0) {
                    min = i;
                }
                max = i;
                if (i == 100000) {
                    System.out.println(min);
                    System.out.println("inf");
                    return;
                }
            }
        }
        
        System.out.println(min);
        System.out.println(max);
    }
    
    static int dfw(int idx, int w) {
        if (w < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][w] < 0) {
            dp[idx][w] = Math.max(dfw(idx - 1, w), dfw(idx - 1, w - weights[idx]) + values[idx]);
        }
        return dp[idx][w];
    }
}
0