結果

問題 No.2028 Even Choice
ユーザー FromBooskaFromBooska
提出日時 2023-04-09 14:52:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,108 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 112,432 KB
最終ジャッジ日時 2024-04-15 05:04:43
合計ジャッジ時間 5,338 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 217 ms
101,948 KB
testcase_01 AC 206 ms
101,936 KB
testcase_02 AC 225 ms
102,388 KB
testcase_03 AC 108 ms
112,432 KB
testcase_04 AC 129 ms
111,496 KB
testcase_05 AC 121 ms
80,896 KB
testcase_06 AC 201 ms
104,188 KB
testcase_07 AC 122 ms
82,824 KB
testcase_08 AC 181 ms
95,592 KB
testcase_09 AC 170 ms
98,004 KB
testcase_10 AC 76 ms
82,860 KB
testcase_11 AC 120 ms
80,204 KB
testcase_12 AC 109 ms
77,656 KB
testcase_13 AC 143 ms
101,240 KB
testcase_14 AC 140 ms
82,972 KB
testcase_15 AC 37 ms
53,288 KB
testcase_16 AC 37 ms
53,996 KB
testcase_17 AC 36 ms
53,160 KB
testcase_18 AC 39 ms
53,620 KB
testcase_19 AC 42 ms
59,228 KB
testcase_20 AC 47 ms
63,588 KB
testcase_21 AC 45 ms
60,832 KB
testcase_22 AC 41 ms
59,640 KB
testcase_23 AC 36 ms
53,560 KB
testcase_24 AC 37 ms
54,240 KB
testcase_25 AC 36 ms
52,568 KB
testcase_26 AC 38 ms
54,336 KB
testcase_27 AC 36 ms
53,188 KB
testcase_28 AC 73 ms
93,136 KB
testcase_29 AC 65 ms
84,016 KB
testcase_30 AC 58 ms
76,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# どれを取るのか、または、どれを取らないのか、の法則性が実験からわからなかった
# 今どれを取るかが今後何を取れるかに影響してしまう、よってdpではなさそう
# 解説見た、一番手前でとるカードは偶数番である必要性がある
# 一番手前が偶数版であればあとは好きなカードを取れる
# 後ろから見ていき、どんどん合計sに加えていく, heapも更新
# 加えた個数がK個に達して、一番手前が偶数番であれば、ans更新
# 加えた個数がK個に達していれば、heapと合計を更新

from heapq import *

N, K = map(int, input().split())
A = list(map(int, input().split()))
s = 0
H = []
heapify(H)
ans = 0

for i in range(N-1, -1, -1):
    a = A[i]
    s += a
    heappush(H, a)
    
    if len(H) == K:
        if i%2 == 1:
            # 一番手前が偶数番号の必要性、0-indexedなので奇数
            ans = max(ans, s)
        # まずans更新してから、heapと合計を更新
        smallest = heappop(H)
        s -= smallest
print(ans)
0