結果

問題 No.247 線形計画問題もどき
ユーザー tentententen
提出日時 2020-12-23 09:57:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 1,780 ms
コンパイル使用メモリ 75,580 KB
実行使用メモリ 57,668 KB
最終ジャッジ日時 2023-10-21 15:03:20
合計ジャッジ時間 6,431 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
57,668 KB
testcase_01 AC 99 ms
56,136 KB
testcase_02 AC 144 ms
57,536 KB
testcase_03 AC 112 ms
57,064 KB
testcase_04 AC 107 ms
57,432 KB
testcase_05 AC 103 ms
57,440 KB
testcase_06 AC 122 ms
57,400 KB
testcase_07 AC 110 ms
56,544 KB
testcase_08 AC 107 ms
56,164 KB
testcase_09 AC 120 ms
57,148 KB
testcase_10 AC 104 ms
56,072 KB
testcase_11 AC 114 ms
57,400 KB
testcase_12 AC 117 ms
57,128 KB
testcase_13 AC 109 ms
57,364 KB
testcase_14 AC 99 ms
56,148 KB
testcase_15 AC 98 ms
56,092 KB
testcase_16 AC 108 ms
57,528 KB
testcase_17 AC 123 ms
57,412 KB
testcase_18 AC 114 ms
56,916 KB
testcase_19 AC 130 ms
57,324 KB
testcase_20 AC 140 ms
55,708 KB
testcase_21 AC 116 ms
57,488 KB
testcase_22 AC 122 ms
57,604 KB
testcase_23 AC 113 ms
56,912 KB
testcase_24 AC 133 ms
57,368 KB
testcase_25 AC 126 ms
56,928 KB
testcase_26 AC 128 ms
57,512 KB
testcase_27 AC 120 ms
57,028 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[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);
        int[] dp = new int[c + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i <= c; i++) {
            for (int j = 0; j < n && i - arr[j] >= 0; j++) {
                if (dp[i - arr[j]] != Integer.MAX_VALUE) {
                    dp[i] = Math.min(dp[i], dp[i - arr[j]] + 1);
                }
            }
        }
        if (dp[c] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(dp[c]);
        }
    }
}
0