結果

問題 No.2028 Even Choice
ユーザー H3PO4
提出日時 2022-08-05 21:58:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 463 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 82,000 KB
実行使用メモリ 108,032 KB
最終ジャッジ日時 2024-09-15 18:31:25
合計ジャッジ時間 5,716 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

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

r = (N - K - 1) // 2 * 2 + 1
h = list(A[r + 1:])
heapq.heapify(h)
while len(h) >= K:
    heapq.heappop(h)
s = sum(h)
ans = 0
for i in range(r, -1, -2):
    ans = max(ans, s + A[i])
    if i >= 0:
        p = heapq.heappushpop(h, A[i])
        s += A[i]
        s -= p
    if i - 1 >= 0:
        p = heapq.heappushpop(h, A[i - 1])
        s += A[i - 1]
        s -= p
print(ans)
0