結果

問題 No.2370 He ate many cakes
ユーザー meruuu61779999
提出日時 2023-06-30 23:02:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 987 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 80,308 KB
最終ジャッジ日時 2024-07-07 10:58:15
合計ジャッジ時間 4,749 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product
from heapq import heappop, heappush


# lst内ケーキの選び方すべてにおける嬉しさのリスト
def H(lst):
    ret = []
    for pro in product((0, 1), repeat=len(lst)):
        val = 0
        for el, pr in zip(lst, pro):
            if pr: val += el
        ret.append(val)
    ret.sort(reverse=True)
    return ret


def f(i, j):
    return C[i] + D[j]


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

mid = N // 2
A_left, A_right = A[:mid], A[mid:]
C, D = H(A_left), H(A_right)

hque = []  # (-value, C_idx, D_idx) を格納する。
E = [0] * len(C)
heappush(hque, (-f(0, 0), 0, 0))

for _ in range(K - 1):
    el = heappop(hque)
    m_val, i, j = el
    E[i] = j + 1
    if j + 1 < len(D):
        if i == 0 or E[i - 1] > E[i]:
            heappush(hque, (-f(i, j + 1), i, j + 1))
    if i + 1 < len(C) and E[i + 1] == j:
        heappush(hque, (-f(i + 1, j), i + 1, j))

m_val, i, j = heappop(hque)
print(-m_val)
0