結果

問題 No.50 おもちゃ箱
ユーザー 👑 rin204rin204
提出日時 2022-09-27 22:19:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 168 ms / 5,000 ms
コード長 1,017 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,200 KB
実行使用メモリ 77,944 KB
最終ジャッジ日時 2023-08-24 04:07:41
合計ジャッジ時間 6,656 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,368 KB
testcase_01 AC 112 ms
77,792 KB
testcase_02 AC 71 ms
71,112 KB
testcase_03 AC 77 ms
71,264 KB
testcase_04 AC 72 ms
71,332 KB
testcase_05 AC 101 ms
77,484 KB
testcase_06 AC 96 ms
76,564 KB
testcase_07 AC 71 ms
71,328 KB
testcase_08 AC 69 ms
71,548 KB
testcase_09 AC 73 ms
71,096 KB
testcase_10 AC 71 ms
71,116 KB
testcase_11 AC 72 ms
71,232 KB
testcase_12 AC 72 ms
71,096 KB
testcase_13 AC 71 ms
71,320 KB
testcase_14 AC 72 ms
71,236 KB
testcase_15 AC 72 ms
71,112 KB
testcase_16 AC 71 ms
71,096 KB
testcase_17 AC 77 ms
76,416 KB
testcase_18 AC 76 ms
76,496 KB
testcase_19 AC 124 ms
77,752 KB
testcase_20 AC 73 ms
71,312 KB
testcase_21 AC 120 ms
77,896 KB
testcase_22 AC 108 ms
77,592 KB
testcase_23 AC 78 ms
76,344 KB
testcase_24 AC 72 ms
71,084 KB
testcase_25 AC 72 ms
71,312 KB
testcase_26 AC 86 ms
76,668 KB
testcase_27 AC 134 ms
77,936 KB
testcase_28 AC 168 ms
77,908 KB
testcase_29 AC 137 ms
77,440 KB
testcase_30 AC 80 ms
76,432 KB
testcase_31 AC 72 ms
71,236 KB
testcase_32 AC 114 ms
77,764 KB
testcase_33 AC 84 ms
76,684 KB
testcase_34 AC 102 ms
77,512 KB
testcase_35 AC 124 ms
77,832 KB
testcase_36 AC 122 ms
77,944 KB
testcase_37 AC 99 ms
76,804 KB
testcase_38 AC 114 ms
77,708 KB
testcase_39 AC 150 ms
77,860 KB
testcase_40 AC 99 ms
76,656 KB
testcase_41 AC 144 ms
77,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
A = list(map(int, input().split()))
m = int(input())
B = list(map(int, input().split()))
A.sort(reverse=True)
B.sort()
ma = B[-1]
C = []
ans = 1 << 30

def ok(C):
    D = C[:]
    D.sort()
    p = 0
    for d in D:
        while p < m and B[p] < d:
            p += 1
        if p == m:
            return False
        p += 1
    return True

def dfs(bit):
    global ans
    if bit == 0:
        if ok(C):
            ans = min(ans, len(C))
        return

    if len(C) >= ans:
        return

    lst = []
    for i in range(n):
        if bit >> i & 1:
            lst.append(i)
    
    a = lst[0]
    le = len(lst) - 1
    for bit2 in range(1 << le):
        tot = A[a]
        dbit = 1 << a
        for j in range(le):
            if bit2 >> j & 1:
                dbit ^= 1 << lst[j + 1]
                tot += A[lst[j + 1]]        
        if tot <= ma:
            C.append(tot)
            dfs(bit ^ dbit)
            C.pop()


dfs((1 << n) - 1)
if ans == 1 << 30:
    ans = -1
print(ans)
0