結果

問題 No.2370 He ate many cakes
ユーザー kutaragi36
提出日時 2023-07-03 23:47:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 916 ms / 2,000 ms
コード長 816 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 136,796 KB
最終ジャッジ日時 2024-07-17 21:35:07
合計ジャッジ時間 6,900 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import heapq

N, K = map(int, input().split())
A = list(map(int, input().split()))


def make(A):
    N = len(A)
    res = []
    for X in itertools.product((True, False), repeat=N):
        t = 0
        for a, x in zip(A, X):
            if x:
                t += a
        res.append(t)
    return res

X = make(A[:N//2])
Y = make(A[N//2:])

X.sort(reverse=True)
Y.sort(reverse=True)
lx = len(X)
ly = len(Y)

used = set()
hq = [(-(X[0]+Y[0]), 0, 0)]
used.add((0, 0))
for _ in range(K):
    p, x, y = heapq.heappop(hq)
    
    if x+1 < lx and not (x+1, y) in used:
        heapq.heappush(hq, (-(X[x+1] + Y[y]), x+1, y))
        used.add((x+1, y))
    
    if y+1 < ly and not (x, y+1) in used:
        heapq.heappush(hq, (-(X[x] + Y[y+1]), x, y+1))
        used.add((x, y+1))
        

print(-p)
0