結果

問題 No.50 おもちゃ箱
コンテスト
ユーザー yaoshimax
提出日時 2015-02-22 15:50:42
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 823 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 145 ms
コンパイル使用メモリ 77,336 KB
最終ジャッジ日時 2025-12-03 14:07:27
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N=int(raw_input())
A=map(int,raw_input().split())
M=int(raw_input())
B=map(int,raw_input().split())
B.sort()
B.reverse()
dp=[(-1,-1) for i in range(1<<N)]
dp[0]=(0,0)

def rec(mask):
    global dp,N,A,M,B
    if dp[mask][0]!=-1:
        return dp[mask]
    a,b=100,100
    for i in range(N):
        if (mask&(1<<i)) != 0:
            nmask = mask-(1<<i)
            n_box,rest=rec(nmask)
            na,nb=100,100
            if rest >= A[i]:
                na,nb=n_box,rest-A[i]
            elif n_box<M and B[n_box] >=A[i]:
                na,nb=n_box+1,B[n_box]-A[i]
            else:
                na,nb=100,100
            if a > na or( a==na and  b < nb ):
                a,b=na,nb
    dp[mask]=(a,b)
    #print format(mask,'b'), a,b
    return a,b


a,b=rec( (1<<N)-1 )
if a >N:
    print -1
else:
    print a

0