結果

問題 No.247 線形計画問題もどき
ユーザー tentententen
提出日時 2021-12-03 08:20:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 1,775 bytes
コンパイル時間 6,468 ms
コンパイル使用メモリ 74,196 KB
実行使用メモリ 102,756 KB
最終ジャッジ日時 2023-09-18 23:25:21
合計ジャッジ時間 6,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
63,888 KB
testcase_01 AC 41 ms
49,208 KB
testcase_02 AC 245 ms
102,756 KB
testcase_03 AC 41 ms
49,148 KB
testcase_04 AC 41 ms
49,236 KB
testcase_05 AC 41 ms
49,780 KB
testcase_06 AC 41 ms
49,228 KB
testcase_07 AC 45 ms
49,508 KB
testcase_08 AC 61 ms
63,432 KB
testcase_09 AC 40 ms
49,516 KB
testcase_10 AC 42 ms
49,276 KB
testcase_11 AC 40 ms
49,272 KB
testcase_12 AC 41 ms
49,264 KB
testcase_13 AC 40 ms
49,276 KB
testcase_14 AC 40 ms
49,368 KB
testcase_15 AC 41 ms
49,588 KB
testcase_16 AC 40 ms
49,160 KB
testcase_17 AC 77 ms
60,740 KB
testcase_18 AC 54 ms
52,348 KB
testcase_19 AC 114 ms
83,340 KB
testcase_20 AC 178 ms
82,380 KB
testcase_21 AC 56 ms
53,896 KB
testcase_22 AC 83 ms
60,316 KB
testcase_23 AC 58 ms
55,364 KB
testcase_24 AC 186 ms
81,940 KB
testcase_25 AC 130 ms
75,892 KB
testcase_26 AC 57 ms
53,760 KB
testcase_27 AC 76 ms
60,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static int[][] dp;
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int c = sc.nextInt();
        int n = sc.nextInt();
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        dp = new int[n][c + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        int ans = dfw(n - 1, c);
        if (ans >= Integer.MAX_VALUE / 2) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static int dfw(int idx, int c) {
        if (c == 0) {
            return 0;
        }
        if (idx < 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (dp[idx][c] < 0) {
            dp[idx][c] = dfw(idx - 1, c);
            if (c >= values[idx]) {
                dp[idx][c] = Math.min(dp[idx][c], dfw(idx, c - values[idx]) + 1);
            }
        }
        return dp[idx][c];
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0