結果

問題 No.50 おもちゃ箱
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-27 21:24:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,323 bytes
コンパイル時間 5,000 ms
コンパイル使用メモリ 74,388 KB
実行使用メモリ 57,976 KB
最終ジャッジ日時 2023-09-15 06:43:03
合計ジャッジ時間 11,027 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,860 KB
testcase_01 AC 129 ms
55,940 KB
testcase_02 AC 127 ms
55,868 KB
testcase_03 AC 126 ms
55,600 KB
testcase_04 AC 126 ms
55,964 KB
testcase_05 AC 127 ms
55,752 KB
testcase_06 AC 128 ms
55,984 KB
testcase_07 AC 126 ms
55,860 KB
testcase_08 AC 127 ms
55,940 KB
testcase_09 AC 128 ms
56,200 KB
testcase_10 AC 125 ms
56,216 KB
testcase_11 AC 126 ms
56,244 KB
testcase_12 AC 124 ms
56,216 KB
testcase_13 AC 126 ms
55,980 KB
testcase_14 AC 128 ms
56,264 KB
testcase_15 AC 126 ms
56,036 KB
testcase_16 AC 126 ms
55,980 KB
testcase_17 AC 123 ms
55,504 KB
testcase_18 AC 124 ms
56,184 KB
testcase_19 AC 124 ms
56,128 KB
testcase_20 AC 123 ms
56,084 KB
testcase_21 AC 126 ms
55,928 KB
testcase_22 AC 127 ms
55,840 KB
testcase_23 AC 123 ms
56,036 KB
testcase_24 AC 123 ms
56,112 KB
testcase_25 AC 127 ms
55,848 KB
testcase_26 AC 124 ms
56,384 KB
testcase_27 AC 124 ms
56,092 KB
testcase_28 AC 127 ms
56,184 KB
testcase_29 WA -
testcase_30 AC 128 ms
56,084 KB
testcase_31 AC 126 ms
55,992 KB
testcase_32 AC 129 ms
53,920 KB
testcase_33 AC 127 ms
56,040 KB
testcase_34 AC 128 ms
55,884 KB
testcase_35 AC 128 ms
56,064 KB
testcase_36 AC 129 ms
56,000 KB
testcase_37 AC 129 ms
56,128 KB
testcase_38 WA -
testcase_39 AC 128 ms
55,936 KB
testcase_40 AC 127 ms
55,596 KB
testcase_41 AC 127 ms
56,264 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[] a = new int[n];
    for(int i = 0; i < n; i++) {
      a[i] = sc.nextInt();
    }
    int m = sc.nextInt();
    int[] b = new int[m];
    for(int i = 0; i < m; i++) {
      b[i] = sc.nextInt();
    }
    Arrays.sort(b);
    int[] sumb = new int[m + 1];
    for(int i = 1; i < m + 1; i++) {
      sumb[i] = sumb[i - 1] + b[m - i];
    }
    int p = (int)Math.pow(2, n);
    int[] dp = new int[p];
    int[] suma = new int[p];
    for(int i = 1; i < p; i++) {
      for(int j = 0; j < n; j++) {
        if((i & (1 << j)) != 0) suma[i] += a[j];
      }
    }

    for(int i = 0; i < p; i++) {
      dp[i] = 100;
    }

    dp[0] = 0;

    for(int i = 1; i < p; i++) {
      for(int j = 0; j < n; j++) {
        if((i & (1 << j)) != 0) {
          int i1 = i - (1 << j);
          if(dp[i1] >= 0) {
            int r = sumb[dp[i1]] - suma[i1];
            if(r >= a[j]) {
              dp[i] = Math.min(dp[i], dp[i1]);
            } else {
              if((dp[i1] < m) && (a[j] <= b[m - dp[i1] - 1]))  dp[i] = Math.min(dp[i], dp[i1] + 1);
            }
          } 
        }
      }
      if(dp[i] == 100) dp[i] = -1;
    }
    System.out.println(dp[p - 1]);
  }
}
0