結果

問題 No.247 線形計画問題もどき
ユーザー mastersatoshimastersatoshi
提出日時 2015-07-18 02:57:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 3,757 ms
コンパイル使用メモリ 71,832 KB
実行使用メモリ 56,052 KB
最終ジャッジ日時 2023-09-21 17:27:15
合計ジャッジ時間 7,577 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
55,432 KB
testcase_01 AC 126 ms
56,052 KB
testcase_02 AC 164 ms
55,412 KB
testcase_03 AC 125 ms
55,876 KB
testcase_04 AC 124 ms
55,176 KB
testcase_05 AC 129 ms
55,652 KB
testcase_06 AC 125 ms
55,468 KB
testcase_07 AC 134 ms
55,364 KB
testcase_08 AC 132 ms
55,332 KB
testcase_09 AC 126 ms
55,524 KB
testcase_10 AC 131 ms
55,380 KB
testcase_11 AC 127 ms
55,576 KB
testcase_12 AC 124 ms
55,696 KB
testcase_13 AC 123 ms
55,620 KB
testcase_14 AC 123 ms
55,048 KB
testcase_15 AC 125 ms
55,472 KB
testcase_16 AC 124 ms
55,520 KB
testcase_17 AC 156 ms
55,848 KB
testcase_18 AC 136 ms
55,384 KB
testcase_19 AC 153 ms
55,400 KB
testcase_20 AC 155 ms
56,012 KB
testcase_21 AC 147 ms
55,928 KB
testcase_22 AC 147 ms
55,212 KB
testcase_23 AC 147 ms
55,380 KB
testcase_24 AC 160 ms
55,856 KB
testcase_25 AC 151 ms
55,796 KB
testcase_26 AC 142 ms
55,404 KB
testcase_27 AC 141 ms
55,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main;
import java.util.*;
public class Main {

    public static final int INF = 1 << 29;
    
    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();
        }
        
        int[] dp = new int[c + 1];
        
        Arrays.fill(dp, INF);
        dp[0] = 0;
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= c; j++) {
                if (j >= a[i]) {
                    dp[j] = Math.min(dp[j], dp[j - a[i]] + 1);
                }
            }
        }
        
        System.out.println(dp[c] == INF ? -1 : dp[c]);
    }

}
0