結果

問題 No.527 ナップサック容量問題
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-11 17:25:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 240 ms / 2,000 ms
コード長 1,200 bytes
コンパイル時間 2,004 ms
コンパイル使用メモリ 77,208 KB
実行使用メモリ 113,124 KB
最終ジャッジ日時 2023-10-24 21:11:16
合計ジャッジ時間 10,915 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 158 ms
59,796 KB
testcase_01 AC 157 ms
59,868 KB
testcase_02 AC 141 ms
57,832 KB
testcase_03 AC 152 ms
59,876 KB
testcase_04 AC 128 ms
57,580 KB
testcase_05 AC 182 ms
72,412 KB
testcase_06 AC 219 ms
89,840 KB
testcase_07 AC 215 ms
86,540 KB
testcase_08 AC 184 ms
68,648 KB
testcase_09 AC 134 ms
57,736 KB
testcase_10 AC 227 ms
89,760 KB
testcase_11 AC 211 ms
86,072 KB
testcase_12 AC 191 ms
79,396 KB
testcase_13 AC 217 ms
89,900 KB
testcase_14 AC 184 ms
68,744 KB
testcase_15 AC 188 ms
79,136 KB
testcase_16 AC 154 ms
59,876 KB
testcase_17 AC 189 ms
72,460 KB
testcase_18 AC 191 ms
79,360 KB
testcase_19 AC 155 ms
59,580 KB
testcase_20 AC 183 ms
68,824 KB
testcase_21 AC 240 ms
111,260 KB
testcase_22 AC 214 ms
86,884 KB
testcase_23 AC 224 ms
93,984 KB
testcase_24 AC 168 ms
66,704 KB
testcase_25 AC 220 ms
86,472 KB
testcase_26 AC 193 ms
79,172 KB
testcase_27 AC 195 ms
79,364 KB
testcase_28 AC 189 ms
79,260 KB
testcase_29 AC 231 ms
113,124 KB
testcase_30 AC 156 ms
62,108 KB
testcase_31 AC 192 ms
79,288 KB
testcase_32 AC 195 ms
79,104 KB
testcase_33 AC 190 ms
79,284 KB
testcase_34 AC 195 ms
79,616 KB
testcase_35 AC 197 ms
79,348 KB
testcase_36 AC 152 ms
59,388 KB
testcase_37 AC 182 ms
72,296 KB
testcase_38 AC 190 ms
79,364 KB
testcase_39 AC 188 ms
79,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    // dp[i][j]は荷物iまでから容量がj以下となるように選んだ時の価値の総和の最大値を表す
    int[][] dp = new int[N][100001];
    int[] v = new int[N];
    int[] w = new int[N];
    for(int i = 0; i < N; i++) {
      v[i] = sc.nextInt();
      w[i] = sc.nextInt(); 
    }
    int V = sc.nextInt();
    for(int j = w[0]; j < 100001; j++) {
      dp[0][j] = v[0];
    }
    for(int i = 1; i < N; i++) {
      for(int j = 1; j < 100001; j++) {
        if(j >= w[i]) {
          dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    int min = 0;
    int max = 0;
    for(int j = 1; j < 100001; j++) {
      if(dp[N - 1][j] == V) {
        min = j;
        break;
      }
    }
    for(int j = 1; j < 100001; j++) {
      if(dp[N - 1][j] > V) {
        max = j - 1;
        break;
      }
    }
    System.out.println(min);
    if(max == 0) {
      System.out.println("inf");
    } else {
      System.out.println(max);
    }
  }
}
0