結果

問題 No.247 線形計画問題もどき
ユーザー nCk_cvnCk_cv
提出日時 2015-08-01 16:58:53
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 865 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 74,472 KB
実行使用メモリ 111,452 KB
最終ジャッジ日時 2023-09-25 00:16:45
合計ジャッジ時間 7,662 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 129 ms
55,892 KB
testcase_02 AC 256 ms
111,452 KB
testcase_03 AC 121 ms
55,548 KB
testcase_04 AC 122 ms
55,556 KB
testcase_05 AC 126 ms
55,952 KB
testcase_06 AC 119 ms
55,672 KB
testcase_07 AC 146 ms
57,848 KB
testcase_08 AC 131 ms
55,748 KB
testcase_09 AC 123 ms
55,992 KB
testcase_10 AC 130 ms
55,648 KB
testcase_11 AC 126 ms
55,916 KB
testcase_12 AC 125 ms
56,120 KB
testcase_13 AC 125 ms
56,136 KB
testcase_14 AC 126 ms
55,668 KB
testcase_15 AC 124 ms
55,540 KB
testcase_16 AC 126 ms
55,832 KB
testcase_17 WA -
testcase_18 AC 137 ms
55,812 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 154 ms
59,912 KB
testcase_22 WA -
testcase_23 AC 151 ms
59,968 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 163 ms
64,332 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) {
					break;
				}
				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