結果

問題 No.50 おもちゃ箱
ユーザー mkawa2
提出日時 2020-01-09 10:49:40
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 947 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-11-23 03:43:31
合計ジャッジ時間 2,985 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))

class Box:
    def __init__(self, cnt, toy):
        self.cnt = cnt
        self.toy = toy

    def __lt__(self, other):
        return self.cnt < other.cnt or (self.cnt == other.cnt and self.toy < other.toy)

def main():
    n=II()
    aa=LI()
    m=II()
    bb=LI()
    bb.sort(reverse=True)
    dp=[Box(100,100)]*(1<<n)
    dp[0]=Box(0,0)
    for i in range(1<<n):
        cnt=dp[i].cnt
        if cnt==100:continue
        toy=dp[i].toy
        for j in range(n):
            if i>>j&1:continue
            a=aa[j]
            if toy+a<=bb[cnt]:value=Box(cnt,toy+a)
            elif cnt+1<=m-1 and a<=bb[cnt+1]:value=Box(cnt+1,a)
            else:continue
            ni=i|1<<j
            if value<dp[ni]:dp[ni]=value
    ans=dp[-1].cnt+1
    if ans==101:print(-1)
    else:print(ans)

main()
0