結果

問題 No.247 線形計画問題もどき
ユーザー suiginginsuigingin
提出日時 2015-07-19 22:50:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 893 bytes
コンパイル時間 4,006 ms
コンパイル使用メモリ 79,440 KB
実行使用メモリ 111,356 KB
最終ジャッジ日時 2023-09-21 17:29:02
合計ジャッジ時間 10,733 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 193 ms
67,832 KB
testcase_01 AC 126 ms
55,544 KB
testcase_02 AC 272 ms
111,356 KB
testcase_03 AC 130 ms
55,852 KB
testcase_04 AC 127 ms
55,988 KB
testcase_05 AC 127 ms
55,552 KB
testcase_06 AC 128 ms
55,812 KB
testcase_07 AC 172 ms
57,972 KB
testcase_08 AC 153 ms
55,828 KB
testcase_09 AC 129 ms
55,544 KB
testcase_10 AC 146 ms
56,484 KB
testcase_11 AC 125 ms
55,624 KB
testcase_12 AC 125 ms
55,552 KB
testcase_13 AC 126 ms
55,576 KB
testcase_14 AC 126 ms
55,876 KB
testcase_15 AC 126 ms
55,800 KB
testcase_16 AC 126 ms
55,668 KB
testcase_17 AC 184 ms
64,852 KB
testcase_18 AC 160 ms
56,564 KB
testcase_19 AC 227 ms
86,088 KB
testcase_20 AC 236 ms
85,940 KB
testcase_21 AC 174 ms
60,864 KB
testcase_22 AC 183 ms
64,788 KB
testcase_23 AC 176 ms
60,460 KB
testcase_24 AC 243 ms
87,628 KB
testcase_25 AC 216 ms
80,460 KB
testcase_26 AC 167 ms
60,440 KB
testcase_27 AC 178 ms
64,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class No_247 {
	Scanner sc = new Scanner(System.in);

	void run() {
		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 + 1][C + 1];

		for (int i = 0; i <= N; i++) {
			for (int j = 1; j <= C; j++) {
				dp[i][j] = Integer.MAX_VALUE / 2;
			}
		}

		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= C; j++) {
				if (j - a[i] >= 0) {
					dp[i + 1][j] = Math.min(dp[i][j], dp[i][j - a[i]] + 1);
					dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i + 1][j - a[i]] + 1);
				} else {
					dp[i + 1][j] = dp[i][j];
				}
			}
		}

		int res = Integer.MAX_VALUE / 2;
		for (int i = 0; i <= N; i++) res = Math.min(res, dp[i][C]);
		System.out.println((res == Integer.MAX_VALUE / 2) ? -1 : res);
	}

	public static void main(String[] args) {
		new No_247().run();
	}
}
0