結果

問題 No.2028 Even Choice
ユーザー FromBooskaFromBooska
提出日時 2023-05-17 21:06:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,600 KB
実行使用メモリ 112,260 KB
最終ジャッジ日時 2024-05-09 05:24:32
合計ジャッジ時間 5,890 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
102,400 KB
testcase_01 AC 210 ms
102,144 KB
testcase_02 AC 238 ms
101,944 KB
testcase_03 AC 114 ms
112,260 KB
testcase_04 AC 134 ms
111,560 KB
testcase_05 AC 132 ms
81,024 KB
testcase_06 AC 214 ms
103,656 KB
testcase_07 AC 128 ms
83,248 KB
testcase_08 AC 193 ms
95,468 KB
testcase_09 AC 182 ms
98,176 KB
testcase_10 AC 80 ms
83,300 KB
testcase_11 AC 132 ms
80,204 KB
testcase_12 AC 118 ms
77,696 KB
testcase_13 AC 152 ms
101,120 KB
testcase_14 AC 147 ms
83,448 KB
testcase_15 AC 39 ms
52,992 KB
testcase_16 AC 39 ms
52,224 KB
testcase_17 AC 40 ms
52,736 KB
testcase_18 AC 42 ms
53,504 KB
testcase_19 AC 45 ms
58,496 KB
testcase_20 AC 51 ms
61,952 KB
testcase_21 AC 49 ms
60,492 KB
testcase_22 AC 46 ms
58,752 KB
testcase_23 AC 40 ms
52,608 KB
testcase_24 AC 40 ms
52,352 KB
testcase_25 AC 40 ms
52,352 KB
testcase_26 AC 39 ms
52,608 KB
testcase_27 AC 39 ms
52,352 KB
testcase_28 AC 79 ms
92,928 KB
testcase_29 AC 67 ms
82,944 KB
testcase_30 AC 61 ms
75,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 理解できていないのでもう一度やる
# 一番手前は偶数番しか取れない、制約から自明
# 一番手前で偶数番をとるので、それ以後は奇数番は繰り上がるし
# それ以後の偶数番は後ろからとればいいので結局とりたいものが取れる
# 前から見ていって、偶数番を取って、その後の最高値K-1個を探索していると間に合わない
# だから後ろから見ていって、最高値K個をheapで管理、その合計値sも管理
# 後ろから見ていってその番号が偶数番のときのみ、ans更新すればいい

from heapq import *
N, K = map(int, input().split())
A = list(map(int, input().split()))
H = []
heapify(H)
s = 0
ans = 0
for i in range(N-1, -1, -1):
    heappush(H, A[i])
    s += A[i]
    
    if len(H) == K:
        if i%2 == 1: #0-indexedなのでこれが偶数番
            ans = max(ans, s)
        
        smallest = heappop(H)
        s -= smallest
print(ans)
0