結果

問題 No.50 おもちゃ箱
ユーザー tcltk
提出日時 2021-05-20 00:06:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 1,475 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 90,352 KB
最終ジャッジ日時 2024-10-10 07:05:53
合計ジャッジ時間 9,516 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 = """2
# 2 3
# 1
# 5
# """
# 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)

    dp = [10**10] * (1<<N)
    dp[0] = 0
    for s in range(1<<N):
        for t in range(1<<N):
            if s & t:
                continue
            s1 = s | t
            size = sum(A[i] for i in range(N) if t & (1<<i))
            if dp[s] < M and size <= B[dp[s]]:
                dp[s1] = min(dp[s1], dp[s] + 1)
    ans = dp[(1<<N)-1]
    if ans == 10**10:
        print(-1)
    else:
        print(ans)

    # 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