結果

問題 No.50 おもちゃ箱
ユーザー chocoruskchocorusk
提出日時 2020-09-10 04:27:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 976 ms / 5,000 ms
コード長 910 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 86,936 KB
実行使用メモリ 86,456 KB
最終ジャッジ日時 2023-08-22 20:38:07
合計ジャッジ時間 17,700 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
77,244 KB
testcase_01 AC 764 ms
86,084 KB
testcase_02 AC 98 ms
76,856 KB
testcase_03 AC 72 ms
71,608 KB
testcase_04 AC 75 ms
71,196 KB
testcase_05 AC 326 ms
86,208 KB
testcase_06 AC 129 ms
78,080 KB
testcase_07 AC 73 ms
71,356 KB
testcase_08 AC 93 ms
76,292 KB
testcase_09 AC 89 ms
76,516 KB
testcase_10 AC 74 ms
71,392 KB
testcase_11 AC 89 ms
76,576 KB
testcase_12 AC 94 ms
76,436 KB
testcase_13 AC 89 ms
76,412 KB
testcase_14 AC 90 ms
76,556 KB
testcase_15 AC 82 ms
76,592 KB
testcase_16 AC 89 ms
76,268 KB
testcase_17 AC 138 ms
78,424 KB
testcase_18 AC 124 ms
78,144 KB
testcase_19 AC 326 ms
80,216 KB
testcase_20 AC 102 ms
76,916 KB
testcase_21 AC 116 ms
77,916 KB
testcase_22 AC 174 ms
78,248 KB
testcase_23 AC 94 ms
76,652 KB
testcase_24 AC 75 ms
71,320 KB
testcase_25 AC 74 ms
71,308 KB
testcase_26 AC 88 ms
76,440 KB
testcase_27 AC 135 ms
78,340 KB
testcase_28 AC 877 ms
86,448 KB
testcase_29 AC 865 ms
86,344 KB
testcase_30 AC 663 ms
86,000 KB
testcase_31 AC 74 ms
71,328 KB
testcase_32 AC 753 ms
86,168 KB
testcase_33 AC 763 ms
86,212 KB
testcase_34 AC 602 ms
86,364 KB
testcase_35 AC 852 ms
86,184 KB
testcase_36 AC 831 ms
86,116 KB
testcase_37 AC 845 ms
86,000 KB
testcase_38 AC 857 ms
86,312 KB
testcase_39 AC 736 ms
86,456 KB
testcase_40 AC 461 ms
86,324 KB
testcase_41 AC 976 ms
86,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

import itertools
n=int(readline())
a=list(map(int, readline().split()))
m=int(readline())
b=list(map(int, readline().split()))
ok=[[False]*(1<<n) for _ in range(m)]
for i in range(1<<n):
    s=sum(x for j, x in enumerate(a) if i&(1<<j))
    for j, y in enumerate(b):
        if s<=y:
            ok[j][i]=True
dp=[False]*(1<<(n+m))
dp[0]=True
for i in range(1<<(n+m)):
    j0=m
    for j in range(m):
        if i&(1<<j):
            j0=j
            break
    else:
        continue
    mask=i&(((1<<n)-1)<<m)
    k=mask
    while k:
        if ok[j0][k>>m] and dp[i^k^(1<<j0)]:
            dp[i]=True
            break
        k=(k-1)&mask
def popcount(x):
    return bin(x).count('1')
ans=[popcount(i) for i in range(1<<m) if dp[(((1<<n)-1)<<m)^i]]
if ans:
    print(min(ans))
else:
    print(-1)
0