結果

問題 No.247 線形計画問題もどき
ユーザー GrenacheGrenache
提出日時 2015-07-17 23:32:49
言語 Java19
(openjdk 21)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 5,919 ms
コンパイル使用メモリ 78,976 KB
実行使用メモリ 111,228 KB
最終ジャッジ日時 2023-09-21 17:24:23
合計ジャッジ時間 9,456 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
67,804 KB
testcase_01 AC 125 ms
56,132 KB
testcase_02 AC 253 ms
111,228 KB
testcase_03 AC 123 ms
55,928 KB
testcase_04 AC 124 ms
56,028 KB
testcase_05 AC 127 ms
55,916 KB
testcase_06 AC 125 ms
55,684 KB
testcase_07 AC 157 ms
58,152 KB
testcase_08 AC 146 ms
56,312 KB
testcase_09 AC 124 ms
55,872 KB
testcase_10 AC 140 ms
56,056 KB
testcase_11 AC 123 ms
56,000 KB
testcase_12 AC 126 ms
55,440 KB
testcase_13 AC 124 ms
55,868 KB
testcase_14 AC 125 ms
56,100 KB
testcase_15 AC 126 ms
56,096 KB
testcase_16 AC 124 ms
55,672 KB
testcase_17 AC 195 ms
65,008 KB
testcase_18 AC 144 ms
55,972 KB
testcase_19 AC 217 ms
85,848 KB
testcase_20 AC 227 ms
85,980 KB
testcase_21 AC 186 ms
60,732 KB
testcase_22 AC 173 ms
64,788 KB
testcase_23 AC 184 ms
61,216 KB
testcase_24 AC 234 ms
87,892 KB
testcase_25 AC 208 ms
78,064 KB
testcase_26 AC 182 ms
60,604 KB
testcase_27 AC 171 ms
64,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;


public class Main_yukicoder247 {

    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();
        }

        final int INF = Integer.MAX_VALUE / 2;
        int[][] dp = new int[n + 1][c + 1];
        for (int i = 0; i <= n; i++) {
        	Arrays.fill(dp[i], INF);
        }
        
        for (int i = 1; i <= n; i++) {
            dp[i][0] = 0;
        	for (int j = 1; j <= c; j++) {
        		dp[i][j] = Math.min(dp[i][j], dp[i - 1][j]);
        		if (j >= a[i - 1]) {
        			dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - a[i - 1]] + 1);
        			dp[i][j] = Math.min(dp[i][j], dp[i][j - a[i - 1]] + 1);
        		}
        	}
        }

        int min = INF;
        for (int i = 1; i <= n; i++) {
        	min = Math.min(min, dp[i][c]);
        }

        if (min == INF) {
        	System.out.println(-1);
        } else {
        	System.out.println(min);
        }

        sc.close();
    }
}
0