結果

問題 No.247 線形計画問題もどき
ユーザー tentententen
提出日時 2021-11-10 20:55:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 78,052 KB
実行使用メモリ 51,372 KB
最終ジャッジ日時 2024-05-01 09:25:04
合計ジャッジ時間 5,088 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
51,372 KB
testcase_01 AC 52 ms
50,252 KB
testcase_02 AC 103 ms
51,340 KB
testcase_03 AC 51 ms
50,456 KB
testcase_04 AC 51 ms
50,248 KB
testcase_05 AC 55 ms
50,276 KB
testcase_06 AC 52 ms
50,356 KB
testcase_07 AC 64 ms
50,608 KB
testcase_08 AC 63 ms
50,708 KB
testcase_09 AC 52 ms
50,364 KB
testcase_10 AC 58 ms
50,332 KB
testcase_11 AC 54 ms
50,456 KB
testcase_12 AC 54 ms
50,288 KB
testcase_13 AC 54 ms
50,356 KB
testcase_14 AC 53 ms
50,344 KB
testcase_15 AC 52 ms
50,384 KB
testcase_16 AC 52 ms
50,368 KB
testcase_17 AC 71 ms
51,116 KB
testcase_18 AC 63 ms
50,904 KB
testcase_19 AC 82 ms
51,220 KB
testcase_20 AC 89 ms
51,308 KB
testcase_21 AC 66 ms
50,448 KB
testcase_22 AC 74 ms
51,092 KB
testcase_23 AC 68 ms
50,764 KB
testcase_24 AC 87 ms
51,220 KB
testcase_25 AC 81 ms
51,224 KB
testcase_26 AC 73 ms
51,224 KB
testcase_27 AC 71 ms
51,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static int[] values;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int c = sc.nextInt();
        int n = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        int[] dp = new int[c + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i <= c; i++) {
            for (int j = 0; j < n; j++) {
                if (i >= values[j] && dp[i - values[j]] != Integer.MAX_VALUE) {
                    dp[i] = Math.min(dp[i], dp[i - values[j]] + 1);
                }
            }
        }
        if (dp[c] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(dp[c]);
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0