結果

問題 No.247 線形計画問題もどき
ユーザー htensaihtensai
提出日時 2020-02-07 11:45:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 2,891 ms
コンパイル使用メモリ 75,352 KB
実行使用メモリ 61,432 KB
最終ジャッジ日時 2023-09-21 19:13:02
合計ジャッジ時間 7,617 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,240 KB
testcase_01 AC 45 ms
49,712 KB
testcase_02 AC 215 ms
55,936 KB
testcase_03 AC 45 ms
49,560 KB
testcase_04 AC 45 ms
49,620 KB
testcase_05 AC 44 ms
49,580 KB
testcase_06 AC 45 ms
49,580 KB
testcase_07 AC 46 ms
49,448 KB
testcase_08 AC 103 ms
55,128 KB
testcase_09 AC 44 ms
49,820 KB
testcase_10 AC 46 ms
49,708 KB
testcase_11 AC 44 ms
49,516 KB
testcase_12 AC 44 ms
49,652 KB
testcase_13 AC 43 ms
49,684 KB
testcase_14 AC 44 ms
49,560 KB
testcase_15 AC 43 ms
49,616 KB
testcase_16 AC 44 ms
49,580 KB
testcase_17 AC 187 ms
57,400 KB
testcase_18 AC 101 ms
55,548 KB
testcase_19 AC 194 ms
56,936 KB
testcase_20 AC 811 ms
59,416 KB
testcase_21 AC 51 ms
49,680 KB
testcase_22 AC 92 ms
53,896 KB
testcase_23 AC 46 ms
49,716 KB
testcase_24 AC 751 ms
61,432 KB
testcase_25 AC 502 ms
59,532 KB
testcase_26 AC 60 ms
51,916 KB
testcase_27 AC 59 ms
50,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int c = Integer.parseInt(br.readLine());
        int n = Integer.parseInt(br.readLine());
        int[] values = new int[n];
        String[] line = br.readLine().split(" ", n);
        for (int i = 0; i < n; i++) {
            values[i] = Integer.parseInt(line[i]);
        }
        Arrays.sort(values);
        int[] maxes = new int[c + 1];
        Arrays.fill(maxes, Integer.MAX_VALUE);
        TreeSet<Integer> next = new TreeSet<>();
        next.add(0);
        maxes[0] = 0;
        while (next.size() > 0) {
            int i = next.pollFirst();
            for (int j = 0; j < n && i + values[j] <= c; j++) {
                maxes[i + values[j]] = Math.min(maxes[i + values[j]], maxes[i] + 1);
                next.add(i + values[j]);
            }
        }
        if (maxes[c] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(maxes[c]);
        }
   }
}
0