結果

問題 No.2028 Even Choice
ユーザー gew1fw
提出日時 2025-06-12 16:57:09
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,039 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 292,472 KB
最終ジャッジ日時 2025-06-12 16:57:17
合計ジャッジ時間 6,991 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    N = int(data[0])
    K = int(data[1])
    A = list(map(int, data[2:2+N]))
    
    current = A.copy()
    heap = []
    for i in range(1, N+1):
        if i % 2 == 0:
            heapq.heappush(heap, (-current[i-1], i-1))  # Using max-heap by negating
    
    sum_k = 0
    for _ in range(K):
        if not heap:
            break
        val, idx = heapq.heappop(heap)
        val = -val
        sum_k += val
        
        # Now, update all elements after idx
        for j in range(idx+1, N):
            current[j-1] = current[j]
        current.pop()
        N -= 1
        
        # Add new even positions
        new_heap = []
        for i in range(1, N+1):
            if i % 2 == 0:
                pos = i-1
                if pos >= len(current):
                    continue
                heapq.heappush(new_heap, (-current[pos], pos))
        heap = new_heap
    
    print(sum_k)

if __name__ == "__main__":
    main()
0