結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 135 ms
57,540 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 183 ms
72,488 KB
testcase_06 AC 223 ms
89,616 KB
testcase_07 WA -
testcase_08 AC 181 ms
68,612 KB
testcase_09 AC 119 ms
56,724 KB
testcase_10 WA -
testcase_11 AC 207 ms
86,212 KB
testcase_12 AC 198 ms
79,372 KB
testcase_13 AC 221 ms
89,980 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 156 ms
62,200 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
        break;
      }
    }
    System.out.println(min);
    if(max == 0) {
      System.out.println("inf");
    } else {
      System.out.println(max);
    }
  }
}
0