結果

問題 No.2028 Even Choice
ユーザー FromBooskaFromBooska
提出日時 2023-05-17 21:06:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 117,956 KB
最終ジャッジ日時 2023-08-21 23:24:51
合計ジャッジ時間 6,449 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 261 ms
106,280 KB
testcase_01 AC 242 ms
106,644 KB
testcase_02 AC 269 ms
106,696 KB
testcase_03 AC 136 ms
116,536 KB
testcase_04 AC 158 ms
117,956 KB
testcase_05 AC 163 ms
81,868 KB
testcase_06 AC 249 ms
101,128 KB
testcase_07 AC 161 ms
84,120 KB
testcase_08 AC 224 ms
95,500 KB
testcase_09 AC 218 ms
95,060 KB
testcase_10 AC 110 ms
83,852 KB
testcase_11 AC 162 ms
81,008 KB
testcase_12 AC 149 ms
78,244 KB
testcase_13 AC 187 ms
97,364 KB
testcase_14 AC 178 ms
84,344 KB
testcase_15 AC 71 ms
71,332 KB
testcase_16 AC 72 ms
71,092 KB
testcase_17 AC 72 ms
71,300 KB
testcase_18 AC 74 ms
71,312 KB
testcase_19 AC 77 ms
75,204 KB
testcase_20 AC 83 ms
75,576 KB
testcase_21 AC 78 ms
75,372 KB
testcase_22 AC 78 ms
75,228 KB
testcase_23 AC 72 ms
71,376 KB
testcase_24 AC 70 ms
71,124 KB
testcase_25 AC 71 ms
71,324 KB
testcase_26 AC 74 ms
71,124 KB
testcase_27 AC 72 ms
71,168 KB
testcase_28 AC 108 ms
95,324 KB
testcase_29 AC 98 ms
89,344 KB
testcase_30 AC 95 ms
83,400 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