結果

問題 No.247 線形計画問題もどき
ユーザー kou6839
提出日時 2015-07-20 13:57:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 578 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 79,536 KB
実行使用メモリ 54,548 KB
最終ジャッジ日時 2024-07-07 11:07:46
合計ジャッジ時間 6,376 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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();
		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;j++){
				if(i-a[j]<0 || dp[i-a[j]]==Integer.MAX_VALUE) continue;
				dp[i]=Math.min(dp[i],dp[i-a[j]]+1);
			}
		}
		if(dp[C]==Integer.MAX_VALUE){
			System.out.println(-1);
		}else{
			System.out.println(dp[C]);
		}
	}
}
0