結果

問題 No.50 おもちゃ箱
ユーザー ckawatakckawatak
提出日時 2019-01-14 22:51:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 199 ms / 5,000 ms
コード長 876 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 76,968 KB
最終ジャッジ日時 2023-09-05 08:39:46
合計ジャッジ時間 8,411 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
76,416 KB
testcase_01 AC 175 ms
76,816 KB
testcase_02 AC 84 ms
76,752 KB
testcase_03 AC 70 ms
71,280 KB
testcase_04 AC 71 ms
71,128 KB
testcase_05 AC 199 ms
76,512 KB
testcase_06 AC 104 ms
76,748 KB
testcase_07 AC 72 ms
71,280 KB
testcase_08 AC 80 ms
76,688 KB
testcase_09 AC 75 ms
76,380 KB
testcase_10 AC 70 ms
71,144 KB
testcase_11 AC 79 ms
75,976 KB
testcase_12 AC 78 ms
76,820 KB
testcase_13 AC 72 ms
71,132 KB
testcase_14 AC 83 ms
76,872 KB
testcase_15 AC 73 ms
71,184 KB
testcase_16 AC 86 ms
76,664 KB
testcase_17 AC 117 ms
76,676 KB
testcase_18 AC 98 ms
76,572 KB
testcase_19 AC 155 ms
76,820 KB
testcase_20 AC 92 ms
76,564 KB
testcase_21 AC 100 ms
76,940 KB
testcase_22 AC 128 ms
76,732 KB
testcase_23 AC 79 ms
76,412 KB
testcase_24 AC 72 ms
70,988 KB
testcase_25 AC 71 ms
71,212 KB
testcase_26 AC 84 ms
76,864 KB
testcase_27 AC 107 ms
76,728 KB
testcase_28 AC 163 ms
76,688 KB
testcase_29 AC 163 ms
76,588 KB
testcase_30 AC 152 ms
76,968 KB
testcase_31 AC 72 ms
71,392 KB
testcase_32 AC 169 ms
76,816 KB
testcase_33 AC 172 ms
76,696 KB
testcase_34 AC 189 ms
76,948 KB
testcase_35 AC 180 ms
76,748 KB
testcase_36 AC 175 ms
76,760 KB
testcase_37 AC 163 ms
76,724 KB
testcase_38 AC 160 ms
76,756 KB
testcase_39 AC 193 ms
76,728 KB
testcase_40 AC 179 ms
76,628 KB
testcase_41 AC 175 ms
76,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int, input().split(' ')))
M = int(input())
B = list(map(int, input().split(' ')))
B = sorted(B, reverse=True)

sum = [0 for _ in range(1<<N)]
for i in range(1<<N):
    for j in range(N):
        if i & (1<<j):
            sum[i] = sum[i] + A[j]

# dp[i][j]: minimum number of boxes
# when boxes are used until i to accommodate j collection of toys
dp = []
for i in range(M+1):
    dp.append([float('inf') for _ in range(1<<N)])

dp[0][0] = 0

for i in range(M):
    for j in range(1<<N):
        if dp[i][j] == float('inf'):
            continue
        for k in range(1<<N):
            if (j & k == 0) and (sum[k] <= B[i]):     
                dp[i+1][j|k] = min(dp[i+1][j|k], dp[i][j]+1)

answer = float('inf')
for i in range(1,M+1):
    answer = min(answer, dp[i][(1<<N)-1])

if answer != float('inf'):
    print(answer)
else:
    print(-1)
0