結果

問題 No.247 線形計画問題もどき
ユーザー nCk_cvnCk_cv
提出日時 2015-08-01 17:06:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 668 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 71,680 KB
実行使用メモリ 56,128 KB
最終ジャッジ日時 2023-09-21 17:31:38
合計ジャッジ時間 7,129 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
55,968 KB
testcase_01 AC 126 ms
55,764 KB
testcase_02 AC 178 ms
56,128 KB
testcase_03 AC 126 ms
55,544 KB
testcase_04 AC 126 ms
55,868 KB
testcase_05 AC 126 ms
55,816 KB
testcase_06 AC 126 ms
55,536 KB
testcase_07 AC 136 ms
55,928 KB
testcase_08 AC 138 ms
56,000 KB
testcase_09 AC 128 ms
55,604 KB
testcase_10 AC 132 ms
55,940 KB
testcase_11 AC 126 ms
56,052 KB
testcase_12 AC 126 ms
55,952 KB
testcase_13 AC 127 ms
55,764 KB
testcase_14 AC 128 ms
56,000 KB
testcase_15 AC 127 ms
55,904 KB
testcase_16 AC 125 ms
55,760 KB
testcase_17 AC 146 ms
55,668 KB
testcase_18 AC 135 ms
55,596 KB
testcase_19 AC 148 ms
55,768 KB
testcase_20 AC 155 ms
55,616 KB
testcase_21 AC 137 ms
55,748 KB
testcase_22 AC 144 ms
55,728 KB
testcase_23 AC 137 ms
55,784 KB
testcase_24 AC 149 ms
55,768 KB
testcase_25 AC 149 ms
55,680 KB
testcase_26 AC 139 ms
55,772 KB
testcase_27 AC 140 ms
55,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main {
	static int INF = 2 << 27;
	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(dp[j] == INF) {
					continue;
				}
				if(j + a[i] > c) {
					break;
				}
				
				dp[j + a[i]] = Math.min((dp[j] + 1),dp[j + a[i]]);
			}
		}
		int min = dp[c];
		if(min == INF) {
			min = -1;
		}
		System.out.println(min);
		
		
		
		
	}
}
0