結果

問題 No.527 ナップサック容量問題
ユーザー mosmos_21mosmos_21
提出日時 2017-06-18 10:30:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,307 bytes
コンパイル時間 2,412 ms
コンパイル使用メモリ 78,272 KB
実行使用メモリ 109,636 KB
最終ジャッジ日時 2024-04-09 19:24:18
合計ジャッジ時間 10,059 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,864 KB
testcase_01 AC 138 ms
56,400 KB
testcase_02 AC 139 ms
56,124 KB
testcase_03 AC 139 ms
56,084 KB
testcase_04 AC 109 ms
53,248 KB
testcase_05 AC 165 ms
69,016 KB
testcase_06 AC 200 ms
86,508 KB
testcase_07 AC 193 ms
83,244 KB
testcase_08 AC 165 ms
65,488 KB
testcase_09 AC 112 ms
54,092 KB
testcase_10 AC 202 ms
86,492 KB
testcase_11 AC 184 ms
82,852 KB
testcase_12 AC 170 ms
75,696 KB
testcase_13 AC 204 ms
88,576 KB
testcase_14 AC 167 ms
65,144 KB
testcase_15 AC 180 ms
75,680 KB
testcase_16 AC 146 ms
55,936 KB
testcase_17 AC 177 ms
76,136 KB
testcase_18 AC 166 ms
76,152 KB
testcase_19 AC 147 ms
56,092 KB
testcase_20 AC 176 ms
65,364 KB
testcase_21 AC 222 ms
109,616 KB
testcase_22 AC 199 ms
83,300 KB
testcase_23 AC 213 ms
90,688 KB
testcase_24 AC 150 ms
62,896 KB
testcase_25 AC 204 ms
83,432 KB
testcase_26 AC 187 ms
75,920 KB
testcase_27 AC 182 ms
75,768 KB
testcase_28 AC 184 ms
76,088 KB
testcase_29 AC 214 ms
109,636 KB
testcase_30 AC 141 ms
59,008 KB
testcase_31 AC 171 ms
75,792 KB
testcase_32 AC 173 ms
75,772 KB
testcase_33 AC 172 ms
75,872 KB
testcase_34 AC 175 ms
75,976 KB
testcase_35 AC 185 ms
75,864 KB
testcase_36 AC 138 ms
56,320 KB
testcase_37 AC 173 ms
69,168 KB
testcase_38 AC 176 ms
75,972 KB
testcase_39 AC 176 ms
75,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
        int N = in.nextInt();
        int[] v = new int[N], w = new int[N];
        int sum = 0;
        for(int i = 0; i < N; i++){
            v[i] = in.nextInt();
            w[i] = in.nextInt();
            sum += w[i];
        }
        int V = in.nextInt();

        int[][] dp = new int[N + 1][100001];
        for(int i = 0; i < N; i++){
            Arrays.fill(dp[i], 0);
        }

        for(int i = 1; i < N + 1; i++){
            for(int j = 0; j < 100001; j++){
                if(j - w[i - 1] >= 0){
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w[i -1]] + v[i - 1]);
                }else{
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        int min = 0, max = 0;
        for(int i = 1; i < dp[N].length; i++){
            if(dp[N][i] == V){
                min = i;
                break;
            }
        }
        for(int i = dp[N].length - 1; i > 0; i--){
            if(dp[N][i] == V){
                max = i;
                break;
            }
        }
        System.out.println(min);
        System.out.println(max > sum ? "inf" : max);
    }
}
0