結果

問題 No.247 線形計画問題もどき
ユーザー nCk_cvnCk_cv
提出日時 2015-08-01 17:00:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 863 bytes
コンパイル時間 2,379 ms
コンパイル使用メモリ 74,500 KB
実行使用メモリ 111,616 KB
最終ジャッジ日時 2023-09-21 17:31:12
合計ジャッジ時間 8,173 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
67,280 KB
testcase_01 AC 125 ms
55,936 KB
testcase_02 AC 267 ms
111,616 KB
testcase_03 AC 125 ms
56,056 KB
testcase_04 AC 125 ms
55,804 KB
testcase_05 AC 126 ms
55,824 KB
testcase_06 AC 127 ms
55,460 KB
testcase_07 AC 154 ms
57,984 KB
testcase_08 AC 136 ms
55,308 KB
testcase_09 AC 127 ms
55,568 KB
testcase_10 AC 134 ms
55,852 KB
testcase_11 AC 126 ms
55,540 KB
testcase_12 AC 128 ms
55,548 KB
testcase_13 AC 129 ms
55,840 KB
testcase_14 AC 128 ms
55,676 KB
testcase_15 AC 127 ms
53,832 KB
testcase_16 AC 127 ms
56,148 KB
testcase_17 AC 177 ms
64,540 KB
testcase_18 AC 136 ms
55,460 KB
testcase_19 AC 240 ms
85,660 KB
testcase_20 AC 247 ms
85,128 KB
testcase_21 AC 171 ms
60,076 KB
testcase_22 AC 177 ms
64,876 KB
testcase_23 AC 169 ms
60,032 KB
testcase_24 AC 246 ms
87,664 KB
testcase_25 AC 222 ms
78,116 KB
testcase_26 AC 175 ms
60,244 KB
testcase_27 AC 170 ms
66,780 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[n][c+1];
		for(int i = 0; i < n; i++) {
			Arrays.fill(dp[i], INF);
			dp[i][0] = 0;
		}
		
		
		
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j <= c; j++) {
				if(i != 0) {
					dp[i][j] = Math.min(dp[i-1][j],dp[i][j]);
				}
				if(dp[i][j] == INF) {
					continue;
				}
				if(j + a[i] > c) {
					continue;
				}
				dp[i][j + a[i]] = Math.min((dp[i][j] + 1),dp[i][j + a[i]]);
			}
		}
		int min = INF;
		for(int i = 0; i < n; i++) {
			min = Math.min(dp[i][c], min);
		}
		if(min == INF) {
			min = -1;
		}
		System.out.println(min);
		
		
		
		
	}
}
0