結果

問題 No.50 おもちゃ箱
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-27 21:22:06
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,305 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 73,768 KB
実行使用メモリ 57,956 KB
最終ジャッジ日時 2023-09-15 11:12:49
合計ジャッジ時間 10,211 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,416 KB
testcase_01 AC 129 ms
55,840 KB
testcase_02 AC 125 ms
55,616 KB
testcase_03 AC 124 ms
55,896 KB
testcase_04 AC 125 ms
55,644 KB
testcase_05 AC 129 ms
55,736 KB
testcase_06 AC 125 ms
55,604 KB
testcase_07 AC 128 ms
55,776 KB
testcase_08 AC 125 ms
55,752 KB
testcase_09 AC 125 ms
55,868 KB
testcase_10 AC 125 ms
55,708 KB
testcase_11 AC 125 ms
55,452 KB
testcase_12 AC 126 ms
55,864 KB
testcase_13 AC 125 ms
56,096 KB
testcase_14 AC 126 ms
55,688 KB
testcase_15 AC 126 ms
55,780 KB
testcase_16 AC 125 ms
55,732 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 127 ms
55,572 KB
testcase_20 AC 127 ms
55,912 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 126 ms
55,788 KB
testcase_25 AC 126 ms
56,112 KB
testcase_26 AC 125 ms
55,256 KB
testcase_27 AC 126 ms
55,952 KB
testcase_28 AC 128 ms
55,528 KB
testcase_29 WA -
testcase_30 AC 127 ms
55,876 KB
testcase_31 AC 126 ms
56,196 KB
testcase_32 AC 128 ms
55,780 KB
testcase_33 AC 128 ms
55,932 KB
testcase_34 AC 128 ms
55,992 KB
testcase_35 AC 128 ms
55,520 KB
testcase_36 AC 126 ms
55,920 KB
testcase_37 AC 127 ms
55,760 KB
testcase_38 WA -
testcase_39 AC 127 ms
55,948 KB
testcase_40 AC 127 ms
55,496 KB
testcase_41 AC 127 ms
55,772 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(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