結果
| 問題 | No.50 おもちゃ箱 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-01-01 20:41:19 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 126 ms / 5,000 ms | 
| コード長 | 591 bytes | 
| コンパイル時間 | 577 ms | 
| コンパイル使用メモリ | 82,304 KB | 
| 実行使用メモリ | 76,172 KB | 
| 最終ジャッジ日時 | 2024-10-10 13:15:26 | 
| 合計ジャッジ時間 | 4,626 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 38 | 
ソースコード
n = int(input())
A = list(map(int,input().split()))
m = int(input())
B = sorted(list(map(int,input().split())),reverse=True)
dp = [0]*(1<<n)
dp[0] = 1
for i,b in enumerate(B,1):
    for bit in range(1<<n)[::-1]:
        if dp[bit] == 0:
            continue
        
        for mask in range(1<<n):
            if bit & mask:
                continue
            count = 0
            for j in range(n):
                if mask >> j & 1:
                    count += A[j]
            if count <= b:
                dp[bit^mask] = 1
    if dp[-1]:
        print(i)
        exit()
print(-1)
            
            
            
        