結果

問題 No.247 線形計画問題もどき
ユーザー kou6839kou6839
提出日時 2015-07-20 13:57:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 578 bytes
コンパイル時間 2,064 ms
コンパイル使用メモリ 71,788 KB
実行使用メモリ 56,152 KB
最終ジャッジ日時 2023-09-21 17:29:44
合計ジャッジ時間 7,317 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
55,612 KB
testcase_01 AC 126 ms
54,068 KB
testcase_02 AC 169 ms
55,728 KB
testcase_03 AC 127 ms
56,152 KB
testcase_04 AC 125 ms
56,128 KB
testcase_05 AC 125 ms
55,652 KB
testcase_06 AC 125 ms
56,076 KB
testcase_07 AC 138 ms
55,612 KB
testcase_08 AC 139 ms
55,996 KB
testcase_09 AC 126 ms
53,960 KB
testcase_10 AC 134 ms
55,804 KB
testcase_11 AC 126 ms
55,744 KB
testcase_12 AC 126 ms
55,580 KB
testcase_13 AC 126 ms
55,588 KB
testcase_14 AC 126 ms
55,672 KB
testcase_15 AC 127 ms
55,680 KB
testcase_16 AC 126 ms
55,552 KB
testcase_17 AC 151 ms
55,704 KB
testcase_18 AC 142 ms
55,616 KB
testcase_19 AC 162 ms
55,552 KB
testcase_20 AC 163 ms
55,896 KB
testcase_21 AC 150 ms
55,856 KB
testcase_22 AC 153 ms
55,752 KB
testcase_23 AC 149 ms
55,808 KB
testcase_24 AC 163 ms
55,724 KB
testcase_25 AC 157 ms
55,612 KB
testcase_26 AC 137 ms
55,776 KB
testcase_27 AC 143 ms
55,620 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[] 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