結果

問題 No.2370 He ate many cakes
ユーザー kutaragi36kutaragi36
提出日時 2023-07-03 23:47:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,050 ms / 2,000 ms
コード長 816 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 86,720 KB
実行使用メモリ 137,728 KB
最終ジャッジ日時 2023-09-24 21:08:00
合計ジャッジ時間 8,594 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,556 KB
testcase_01 AC 73 ms
71,504 KB
testcase_02 AC 75 ms
71,368 KB
testcase_03 AC 477 ms
97,832 KB
testcase_04 AC 73 ms
71,284 KB
testcase_05 AC 73 ms
71,528 KB
testcase_06 AC 74 ms
71,076 KB
testcase_07 AC 75 ms
71,428 KB
testcase_08 AC 76 ms
71,432 KB
testcase_09 AC 75 ms
71,564 KB
testcase_10 AC 219 ms
80,348 KB
testcase_11 AC 324 ms
87,976 KB
testcase_12 AC 656 ms
109,172 KB
testcase_13 AC 728 ms
120,576 KB
testcase_14 AC 348 ms
89,596 KB
testcase_15 AC 993 ms
137,728 KB
testcase_16 AC 1,050 ms
132,284 KB
testcase_17 AC 976 ms
132,768 KB
testcase_18 AC 693 ms
118,552 KB
testcase_19 AC 89 ms
76,188 KB
権限があれば一括ダウンロードができます

ソースコード

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