結果

問題 No.286 Modulo Discount Store
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-25 22:30:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 968 bytes
コンパイル時間 1,999 ms
コンパイル使用メモリ 74,304 KB
実行使用メモリ 56,632 KB
最終ジャッジ日時 2023-09-15 00:08:21
合計ジャッジ時間 8,583 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
56,380 KB
testcase_01 AC 114 ms
56,228 KB
testcase_02 AC 133 ms
55,608 KB
testcase_03 AC 127 ms
53,384 KB
testcase_04 AC 115 ms
56,120 KB
testcase_05 AC 117 ms
55,528 KB
testcase_06 AC 134 ms
56,148 KB
testcase_07 AC 116 ms
55,924 KB
testcase_08 AC 117 ms
55,764 KB
testcase_09 AC 117 ms
56,024 KB
testcase_10 AC 128 ms
55,800 KB
testcase_11 AC 115 ms
55,508 KB
testcase_12 AC 119 ms
55,464 KB
testcase_13 AC 128 ms
56,276 KB
testcase_14 AC 113 ms
55,716 KB
testcase_15 AC 114 ms
56,312 KB
testcase_16 AC 113 ms
55,976 KB
testcase_17 AC 132 ms
55,788 KB
testcase_18 AC 132 ms
56,444 KB
testcase_19 AC 119 ms
56,072 KB
testcase_20 AC 114 ms
55,740 KB
testcase_21 AC 117 ms
56,232 KB
testcase_22 AC 119 ms
56,064 KB
testcase_23 AC 127 ms
56,072 KB
testcase_24 AC 116 ms
56,264 KB
testcase_25 AC 116 ms
56,164 KB
testcase_26 AC 116 ms
56,032 KB
testcase_27 AC 117 ms
56,132 KB
testcase_28 AC 131 ms
56,024 KB
testcase_29 AC 112 ms
55,952 KB
testcase_30 AC 117 ms
56,244 KB
testcase_31 AC 118 ms
56,184 KB
testcase_32 AC 115 ms
55,816 KB
testcase_33 AC 119 ms
55,968 KB
testcase_34 AC 112 ms
56,116 KB
testcase_35 AC 115 ms
55,496 KB
testcase_36 AC 116 ms
56,148 KB
testcase_37 AC 120 ms
55,960 KB
testcase_38 AC 113 ms
55,792 KB
testcase_39 AC 132 ms
56,632 KB
testcase_40 AC 116 ms
55,796 KB
testcase_41 AC 115 ms
55,744 KB
testcase_42 AC 115 ms
56,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] m = new int[n];
    for(int i = 0; i < n; i++) {
      m[i] = sc.nextInt();
    }
    int p = (int)Math.pow(2, n);
    int[] sum = new int[p];
    int[] dp = new int[p];
    for(int i = 1; i < p; i++) {
      for(int j = 0; j < n; j++) {
        if((i & (1 << j)) != 0) sum[i] = (sum[i] + m[j]) % 1000;
      }
    }
    for(int i = 0; i < p; i++) {
      dp[i] = Integer.MAX_VALUE;
    }
    for(int i = 0; i < n; i++) {
      dp[(int)(1 << i)] = m[i];
    }
    for(int i = 1; i < p; i++) {
      if(dp[i] == Integer.MAX_VALUE) {
      for(int j = 0; j < n; j++) {
        if((i & (1 << j)) != 0) {
          int i1 = i - (1 << j);
          int m1 = (int)Math.max(m[j] - sum[i1], 0);
          dp[i] = Math.min(dp[i], dp[i1] + m1);
        }
      }
      }
    }
    System.out.println(dp[p - 1]);
  }
}
0