結果

問題 No.385 カップ麺生活
ユーザー tentententen
提出日時 2020-09-29 14:33:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 381 ms / 2,000 ms
コード長 1,270 bytes
コンパイル時間 2,122 ms
コンパイル使用メモリ 77,792 KB
実行使用メモリ 54,616 KB
最終ジャッジ日時 2024-04-15 07:42:04
合計ジャッジ時間 8,497 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
54,448 KB
testcase_01 AC 131 ms
54,012 KB
testcase_02 AC 131 ms
54,152 KB
testcase_03 AC 132 ms
54,000 KB
testcase_04 AC 164 ms
54,196 KB
testcase_05 AC 132 ms
54,312 KB
testcase_06 AC 152 ms
54,492 KB
testcase_07 AC 137 ms
54,292 KB
testcase_08 AC 136 ms
54,120 KB
testcase_09 AC 132 ms
54,180 KB
testcase_10 AC 381 ms
54,348 KB
testcase_11 AC 131 ms
54,344 KB
testcase_12 AC 146 ms
54,052 KB
testcase_13 AC 158 ms
54,236 KB
testcase_14 AC 142 ms
53,660 KB
testcase_15 AC 160 ms
54,268 KB
testcase_16 AC 132 ms
54,140 KB
testcase_17 AC 165 ms
54,264 KB
testcase_18 AC 139 ms
53,980 KB
testcase_19 AC 136 ms
54,064 KB
testcase_20 AC 156 ms
54,616 KB
testcase_21 AC 164 ms
54,428 KB
testcase_22 AC 163 ms
54,452 KB
testcase_23 AC 162 ms
54,384 KB
testcase_24 AC 175 ms
54,504 KB
testcase_25 AC 130 ms
54,068 KB
testcase_26 AC 133 ms
53,964 KB
testcase_27 AC 139 ms
54,228 KB
testcase_28 AC 139 ms
53,960 KB
testcase_29 AC 134 ms
54,132 KB
testcase_30 AC 163 ms
54,276 KB
testcase_31 AC 134 ms
54,372 KB
testcase_32 AC 134 ms
54,308 KB
testcase_33 AC 168 ms
54,372 KB
testcase_34 AC 133 ms
53,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);
        int[] dp = new int[m + 1];
        for (int i = 1; arr[0] * i <= m; i++) {
            dp[arr[0] * i] = i;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j + arr[i] <= m; j++) {
                if (j != 0 && dp[j] == 0) {
                    continue;
                }
                for (int k = 1; j + arr[i] * k <= m; k++) {
                    dp[j + arr[i] * k] = Math.max(dp[j + arr[i] * k], dp[j] + k);
                }
            }
        }
        long ans = 0;
        for (int i = 0; i <= m; i++) {
            ans = Math.max(ans, dp[i]);
        }
        boolean[] isNotPrime = new boolean[m + 1];
        for (int i = 2; i < m; i++) {
            if (!isNotPrime[i]) {
                ans += dp[m - i];
                for (int j = 2; i * j <= m; j++) {
                    isNotPrime[i * j] = true;
                }
            }
        }
        System.out.println(ans);
    }
}
0