結果

問題 No.247 線形計画問題もどき
ユーザー tenten
提出日時 2021-11-10 20:55:48
言語 Java
(openjdk 23)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 78,180 KB
実行使用メモリ 51,692 KB
最終ジャッジ日時 2024-11-21 12:45:38
合計ジャッジ時間 5,123 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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