結果

問題 No.50 おもちゃ箱
ユーザー tcltk
提出日時 2021-05-19 23:49:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 804 ms / 5,000 ms
コード長 1,038 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 89,128 KB
最終ジャッジ日時 2024-10-10 05:45:47
合計ジャッジ時間 15,434 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#region Header
#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq


def input():
    return sys.stdin.readline()[:-1]


# sys.setrecursionlimit(1000000)
#endregion

# _INPUT = """9
# 10 18 19 12 8 12 1 1 4
# 1
# 1
# """
# sys.stdin = io.StringIO(_INPUT)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    M = int(input())
    B = list(map(int, input().split()))
    B.sort(reverse=True)

    ans = 100
    for pat in itertools.permutations(A):
        i = 0
        cap = B[0]
        ok = True
        for item in pat:
            if item <= cap:
                cap -= item
            else:
                i += 1
                if i >= M or B[i] < item:
                    ok = False
                    break
                cap = B[i] - item
        if ok:
            ans = min(ans, i+1)

    if ans == 100:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()
0