結果

問題 No.2028 Even Choice
ユーザー ShirotsumeShirotsume
提出日時 2022-06-14 16:12:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 1,033 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 112,276 KB
最終ジャッジ日時 2024-04-15 11:52:28
合計ジャッジ時間 7,042 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
102,144 KB
testcase_01 AC 281 ms
101,900 KB
testcase_02 AC 310 ms
102,048 KB
testcase_03 AC 139 ms
112,276 KB
testcase_04 AC 175 ms
111,824 KB
testcase_05 AC 184 ms
81,508 KB
testcase_06 AC 287 ms
103,592 KB
testcase_07 AC 178 ms
83,556 KB
testcase_08 AC 253 ms
95,196 KB
testcase_09 AC 251 ms
98,248 KB
testcase_10 AC 115 ms
83,168 KB
testcase_11 AC 192 ms
80,780 KB
testcase_12 AC 168 ms
77,844 KB
testcase_13 AC 190 ms
101,148 KB
testcase_14 AC 208 ms
83,220 KB
testcase_15 AC 42 ms
53,476 KB
testcase_16 AC 44 ms
53,928 KB
testcase_17 AC 44 ms
54,616 KB
testcase_18 AC 49 ms
54,316 KB
testcase_19 AC 54 ms
59,696 KB
testcase_20 AC 60 ms
63,376 KB
testcase_21 AC 58 ms
61,264 KB
testcase_22 AC 53 ms
59,708 KB
testcase_23 AC 45 ms
52,944 KB
testcase_24 AC 45 ms
53,564 KB
testcase_25 AC 46 ms
53,348 KB
testcase_26 AC 46 ms
54,060 KB
testcase_27 AC 46 ms
53,224 KB
testcase_28 AC 96 ms
95,416 KB
testcase_29 AC 84 ms
84,980 KB
testcase_30 AC 76 ms
78,616 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 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