結果

問題 No.2028 Even Choice
ユーザー ShirotsumeShirotsume
提出日時 2022-06-14 15:55:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,014 bytes
コンパイル時間 405 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 112,480 KB
最終ジャッジ日時 2024-10-02 12:21:04
合計ジャッジ時間 5,996 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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:
            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