結果

問題 No.2028 Even Choice
ユーザー FromBooskaFromBooska
提出日時 2023-08-09 18:58:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 834 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 112,740 KB
最終ジャッジ日時 2024-04-27 13:41:13
合計ジャッジ時間 4,918 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 231 ms
102,016 KB
testcase_01 AC 214 ms
101,752 KB
testcase_02 AC 244 ms
102,204 KB
testcase_03 AC 115 ms
112,740 KB
testcase_04 AC 138 ms
111,312 KB
testcase_05 AC 133 ms
80,888 KB
testcase_06 AC 216 ms
103,804 KB
testcase_07 AC 131 ms
83,216 KB
testcase_08 AC 197 ms
95,324 KB
testcase_09 AC 183 ms
98,048 KB
testcase_10 AC 82 ms
82,940 KB
testcase_11 AC 133 ms
80,068 KB
testcase_12 AC 117 ms
77,700 KB
testcase_13 AC 155 ms
101,384 KB
testcase_14 AC 149 ms
83,076 KB
testcase_15 AC 41 ms
53,896 KB
testcase_16 AC 40 ms
53,148 KB
testcase_17 AC 40 ms
54,136 KB
testcase_18 AC 42 ms
54,800 KB
testcase_19 AC 45 ms
59,748 KB
testcase_20 AC 51 ms
61,548 KB
testcase_21 AC 50 ms
62,036 KB
testcase_22 AC 47 ms
60,028 KB
testcase_23 AC 40 ms
52,640 KB
testcase_24 AC 41 ms
53,632 KB
testcase_25 AC 40 ms
52,756 KB
testcase_26 AC 40 ms
52,404 KB
testcase_27 AC 39 ms
53,024 KB
testcase_28 AC 84 ms
93,320 KB
testcase_29 AC 74 ms
83,936 KB
testcase_30 AC 61 ms
75,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 一番手前の数字は偶数番目である必要ある
# それ以後で元々偶数番なら最初にとればいいので取れる
# それ以後で奇数番なら最初の偶数番を取ってから取ればいいので取れる
# つまり一番手前が偶数番ならば自由にとれる
# 後ろからheapで高いものを管理
# 常に最大値K-1個をheapに維持する
# 偶数番をとったときにans更新

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

from heapq import *
H = []
heapify(H)
s = 0
ans = 0
for i in range(N-1, -1, -1):
    a = A[i]
    heappush(H, a)
    s += a
    
    if len(H) == K:
        if i%2 == 1: 
            ans = max(ans, s)        
        smallest = heappop(H)
        s -= smallest
        # 常に最大値K-1個をheapに維持する
    
print(ans)

0