結果

問題 No.527 ナップサック容量問題
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2017-06-10 03:21:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,659 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 77,188 KB
実行使用メモリ 58,044 KB
最終ジャッジ日時 2023-10-24 05:30:17
合計ジャッジ時間 11,022 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
57,444 KB
testcase_01 AC 138 ms
57,552 KB
testcase_02 AC 143 ms
57,648 KB
testcase_03 AC 137 ms
57,400 KB
testcase_04 AC 134 ms
57,404 KB
testcase_05 AC 166 ms
57,752 KB
testcase_06 AC 188 ms
57,696 KB
testcase_07 AC 169 ms
55,780 KB
testcase_08 AC 167 ms
57,760 KB
testcase_09 AC 144 ms
57,384 KB
testcase_10 AC 179 ms
57,688 KB
testcase_11 AC 176 ms
57,920 KB
testcase_12 AC 170 ms
56,180 KB
testcase_13 AC 196 ms
57,824 KB
testcase_14 AC 163 ms
57,540 KB
testcase_15 AC 163 ms
57,544 KB
testcase_16 AC 140 ms
57,788 KB
testcase_17 AC 161 ms
57,668 KB
testcase_18 AC 168 ms
57,848 KB
testcase_19 AC 142 ms
57,460 KB
testcase_20 AC 163 ms
57,672 KB
testcase_21 AC 200 ms
57,524 KB
testcase_22 AC 171 ms
57,652 KB
testcase_23 AC 182 ms
57,948 KB
testcase_24 AC 157 ms
57,440 KB
testcase_25 AC 174 ms
57,552 KB
testcase_26 AC 165 ms
57,812 KB
testcase_27 AC 171 ms
57,736 KB
testcase_28 AC 163 ms
57,736 KB
testcase_29 AC 201 ms
57,788 KB
testcase_30 AC 143 ms
57,676 KB
testcase_31 AC 145 ms
57,680 KB
testcase_32 AC 146 ms
57,504 KB
testcase_33 AC 146 ms
57,464 KB
testcase_34 AC 147 ms
57,584 KB
testcase_35 AC 164 ms
57,660 KB
testcase_36 AC 139 ms
57,168 KB
testcase_37 AC 164 ms
57,736 KB
testcase_38 AC 168 ms
58,044 KB
testcase_39 AC 165 ms
57,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int [] v = new int [N];
        int [] w = new int [N];
        int W = 0;
        for(int i=0; i<N; i++){
            v[i] = sc.nextInt();
            w[i] = sc.nextInt();
            W+=w[i];
        }
        int V = sc.nextInt();
        solve(N,v,w,W,V);
    }
    static void solve(int N, int[]v, int[]w, int W, int V){
        int [] sum = new int [W+1];
        boolean [] boo = new boolean [W+1];
        boo[0] = true;
        for(int i=0; i<N; i++){
            for(int j=W; j>=0; j--){
                if(boo[j]){
                    boo[j+w[i]] = true;
                    sum[j+w[i]] = Math.max(sum[j+w[i]],sum[j]+v[i]);
                }
            }
        }
        for(int a=0; a<=W; a++){
            for(int b=a+1; b<=W; b++){
                if(sum[a]<=sum[b]){
                    break;
                }else{
                    sum[b]=sum[a];
                }
            }
        }
        seachMin(sum,V);
        seachMax(sum,V);
    }
    static void seachMin(int[]sum, int V){
        for(int i=1; i<=sum.length; i++){
            if(sum[i]==V){
                System.out.println(i);
                break;
            }
        }
    }
    static void seachMax(int[]sum, int V){
        if(sum[sum.length-1]==V){
            System.out.println("inf");
            System.exit(0);
        }
        for(int i=sum.length-2; i>=0; i--){
            if(sum[i]==V){
                System.out.println(i);
                break;
            }
        }
    }
}
0