結果

問題 No.2028 Even Choice
ユーザー Shirotsume
提出日時 2022-07-04 01:09:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 366 ms / 2,000 ms
コード長 1,033 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 33,392 KB
最終ジャッジ日時 2024-12-17 17:29:58
合計ジャッジ時間 7,052 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class Heap():
    def __init__(self, maxi = False):
        self.sum = 0
        self.al = []
        self.maxi = maxi
        self.len = 0

    def __len__(self):
        return len(self.al)

    def add(self, x):
        if self.maxi:
            heapq.heappush(self.al, -x)
        else:
            heapq.heappush(self.al, x)
        self.sum += x
    
    def pop(self):
        if self.maxi:
            now = heapq.heappop(self.al)
            self.sum += now
            return -now
        else:
            now = heapq.heappop(self.al)
            self.sum -= now
            return now

    def top(self):
        now = self.pop()
        self.add(now)
        return now
def solve(n, k, a):
    Q = Heap()
    ans = 0
    for i in range(n - 1, -1, -1):
        if i % 2 == 1 and len(Q) == k - 1:
            ans = max(ans, a[i] + Q.sum)
        Q.add(a[i])
        if len(Q) >= k:
            Q.pop()
    return ans

n, k = map(int,input().split())

a = list(map(int,input().split()))

print(solve(n, k , a))
0