結果
| 問題 | No.50 おもちゃ箱 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-07-09 16:22:20 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,056 ms / 5,000 ms | 
| コード長 | 733 bytes | 
| コンパイル時間 | 168 ms | 
| コンパイル使用メモリ | 82,112 KB | 
| 実行使用メモリ | 76,668 KB | 
| 最終ジャッジ日時 | 2024-07-23 13:32:52 | 
| 合計ジャッジ時間 | 8,776 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 38 | 
ソースコード
def next_permutation(arr):
    i = len(arr) - 2
    while i >= 0 and arr[i] >= arr[i + 1]:
        i -= 1
    if i == -1:
        return False
    j = len(arr) - 1
    while arr[j] <= arr[i]:
        j -= 1
    arr[i], arr[j] = arr[j], arr[i]
    arr[i + 1:] = reversed(arr[i + 1:])
    return True
from itertools import permutations
n = int(input())
A = sorted(map(int, input().split()))
m = int(input()) + 1
B = list(reversed(sorted(map(int, input().split()))))
a = m
while True:
    T, i, j = B[:], 0, 0
    while i < n and j < m - 1:
        if T[j] >= A[i]:
            T[j] -= A[i]
            i += 1
        else:
            j += 1
    a = min(a, j + 1)
    if not next_permutation(A):
        break
print(a if a < m else -1)
            
            
            
        