結果

問題 No.247 線形計画問題もどき
ユーザー takeya_okinotakeya_okino
提出日時 2017-07-05 20:47:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 700 bytes
コンパイル時間 4,593 ms
コンパイル使用メモリ 71,560 KB
実行使用メモリ 57,652 KB
最終ジャッジ日時 2023-09-21 18:32:28
合計ジャッジ時間 7,565 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
56,000 KB
testcase_01 AC 124 ms
55,844 KB
testcase_02 AC 159 ms
55,700 KB
testcase_03 AC 124 ms
56,004 KB
testcase_04 AC 125 ms
57,652 KB
testcase_05 AC 124 ms
56,012 KB
testcase_06 AC 122 ms
55,856 KB
testcase_07 AC 135 ms
55,700 KB
testcase_08 AC 134 ms
55,448 KB
testcase_09 AC 123 ms
55,800 KB
testcase_10 AC 132 ms
55,968 KB
testcase_11 AC 123 ms
55,660 KB
testcase_12 AC 124 ms
55,896 KB
testcase_13 AC 124 ms
55,436 KB
testcase_14 AC 123 ms
55,924 KB
testcase_15 AC 123 ms
55,480 KB
testcase_16 AC 124 ms
55,552 KB
testcase_17 AC 145 ms
55,780 KB
testcase_18 AC 136 ms
56,300 KB
testcase_19 AC 167 ms
55,736 KB
testcase_20 AC 160 ms
55,888 KB
testcase_21 AC 149 ms
55,860 KB
testcase_22 AC 151 ms
56,068 KB
testcase_23 AC 151 ms
55,644 KB
testcase_24 AC 163 ms
55,904 KB
testcase_25 AC 156 ms
56,468 KB
testcase_26 AC 135 ms
55,956 KB
testcase_27 AC 141 ms
56,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int C = sc.nextInt();
    int N = sc.nextInt();
    int[] a = new int[N];
    for(int i = 0; i < N; i++) {
      a[i] = sc.nextInt();
    }
    // dp[i]はf=iの制約下でのΣxの最小値を表す(解がなければ-1とする)
    int[] dp = new int[C + 1];
    for(int i = 1; i < C + 1; i++) {
      int min = Integer.MAX_VALUE;
      for(int j = 0; j < N; j++) {
        if(a[j] <= i && dp[i - a[j]] != -1) min = Math.min(min, 1 + dp[i - a[j]]);
      }
      if(min == Integer.MAX_VALUE) min = -1;
      dp[i] = min;
    }
    System.out.println(dp[C]);
  }
}
0