結果

問題 No.50 おもちゃ箱
ユーザー 👑 colognecologne
提出日時 2022-01-24 19:55:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 917 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 77,984 KB
最終ジャッジ日時 2023-08-21 11:03:28
合計ジャッジ時間 6,218 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
75,640 KB
testcase_01 AC 93 ms
76,452 KB
testcase_02 AC 72 ms
71,308 KB
testcase_03 AC 70 ms
71,180 KB
testcase_04 AC 72 ms
71,368 KB
testcase_05 AC 92 ms
76,536 KB
testcase_06 AC 91 ms
76,368 KB
testcase_07 AC 72 ms
71,368 KB
testcase_08 AC 70 ms
71,300 KB
testcase_09 AC 71 ms
71,424 KB
testcase_10 AC 72 ms
71,344 KB
testcase_11 AC 71 ms
71,304 KB
testcase_12 AC 70 ms
71,384 KB
testcase_13 AC 71 ms
71,188 KB
testcase_14 AC 73 ms
71,368 KB
testcase_15 AC 73 ms
71,344 KB
testcase_16 AC 73 ms
71,252 KB
testcase_17 AC 89 ms
76,580 KB
testcase_18 AC 85 ms
76,284 KB
testcase_19 AC 95 ms
76,364 KB
testcase_20 AC 81 ms
75,932 KB
testcase_21 AC 89 ms
76,248 KB
testcase_22 AC 96 ms
76,664 KB
testcase_23 AC 81 ms
76,444 KB
testcase_24 AC 76 ms
71,408 KB
testcase_25 AC 75 ms
71,380 KB
testcase_26 AC 82 ms
75,744 KB
testcase_27 AC 94 ms
76,272 KB
testcase_28 AC 94 ms
76,392 KB
testcase_29 AC 91 ms
76,372 KB
testcase_30 AC 83 ms
76,448 KB
testcase_31 AC 71 ms
71,144 KB
testcase_32 AC 93 ms
76,484 KB
testcase_33 AC 92 ms
76,604 KB
testcase_34 AC 105 ms
77,884 KB
testcase_35 AC 93 ms
76,588 KB
testcase_36 AC 89 ms
76,372 KB
testcase_37 AC 91 ms
76,348 KB
testcase_38 AC 86 ms
76,484 KB
testcase_39 AC 103 ms
77,984 KB
testcase_40 AC 108 ms
77,428 KB
testcase_41 AC 89 ms
76,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys


def input(): return sys.stdin.readline().rstrip()


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

    S = [0]*(1 << N)
    for i in range(1 << N):
        for j in range(N):
            if i & (1 << j):
                S[i] += A[j]

    dp = [[None]*(M+1) for i in range(1 << N)]

    def solve(am, b):
        if dp[am][b] is not None:
            return dp[am][b]
        if am == 0:
            return 0
        if b == M:
            return M+1

        ans = M+1
        j = am
        while j > 0:
            if am & (j & -j) != 0 and S[j] <= B[b]:
                ans = min(ans, solve(am-j, b+1)+1)
            j = (j-1) & am

        dp[am][b] = ans
        return ans

    ans = solve((1 << N)-1, 0)
    if ans == M+1:
        ans = -1
    print(ans)


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