結果

問題 No.2028 Even Choice
ユーザー ShirotsumeShirotsume
提出日時 2022-06-14 15:55:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 1,014 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 112,984 KB
最終ジャッジ日時 2024-04-10 10:31:46
合計ジャッジ時間 5,872 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 256 ms
101,828 KB
testcase_01 AC 226 ms
101,948 KB
testcase_02 AC 257 ms
102,024 KB
testcase_03 AC 117 ms
112,984 KB
testcase_04 AC 140 ms
111,052 KB
testcase_05 AC 140 ms
80,760 KB
testcase_06 AC 231 ms
103,768 KB
testcase_07 AC 143 ms
82,996 KB
testcase_08 AC 217 ms
95,548 KB
testcase_09 AC 205 ms
98,280 KB
testcase_10 AC 96 ms
83,112 KB
testcase_11 AC 160 ms
80,348 KB
testcase_12 AC 139 ms
77,596 KB
testcase_13 AC 157 ms
100,696 KB
testcase_14 AC 166 ms
82,804 KB
testcase_15 AC 38 ms
53,596 KB
testcase_16 AC 38 ms
53,460 KB
testcase_17 AC 38 ms
52,616 KB
testcase_18 AC 41 ms
54,188 KB
testcase_19 AC 45 ms
60,536 KB
testcase_20 AC 52 ms
62,780 KB
testcase_21 AC 48 ms
61,584 KB
testcase_22 AC 44 ms
59,796 KB
testcase_23 AC 38 ms
52,752 KB
testcase_24 AC 39 ms
53,440 KB
testcase_25 AC 38 ms
53,476 KB
testcase_26 AC 38 ms
53,568 KB
testcase_27 AC 37 ms
53,836 KB
testcase_28 AC 87 ms
95,460 KB
testcase_29 AC 75 ms
85,644 KB
testcase_30 AC 64 ms
77,944 KB
権限があれば一括ダウンロードができます

ソースコード

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