結果
| 問題 | No.50 おもちゃ箱 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2025-06-16 15:43:08 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 158 ms / 5,000 ms | 
| コード長 | 555 bytes | 
| コンパイル時間 | 359 ms | 
| コンパイル使用メモリ | 12,160 KB | 
| 実行使用メモリ | 10,496 KB | 
| 最終ジャッジ日時 | 2025-06-16 15:43:13 | 
| 合計ジャッジ時間 | 4,587 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 38 | 
ソースコード
import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
N=int(readline())
*A,=map(int,readline().split())
M=int(readline())
*B,=map(int,read().split())
B.sort(reverse=True)
sum_size=[0]
for x in A:
  sum_size+=[x+y for y in sum_size]
full=1<<N
dp=[0]*full
dp[0]=1
ans=-1
for n, box_size in enumerate(B,1):
  ndp=[0]*full
  for i in range(1<<N):
    j=i
    while True:
      if sum_size[j]<=box_size and dp[i^j]:
        ndp[i]=1
        break
      if not j: break
      j=(j-1)&i
  dp=ndp
  if dp[-1]:
    ans=n
    break
print(ans)
            
            
            
        