結果

問題 No.1248 Kth Sum
ユーザー chineristAC
提出日時 2020-10-02 22:12:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 550 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 106,880 KB
最終ジャッジ日時 2024-07-17 21:25:37
合計ジャッジ時間 5,969 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11 WA * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush,heappop,heapify

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

if K==1:
    exit(print(A[0]))

ans = 10**18
que = []
S = 0
for i in range(1,N+1):
    if i*K>N:
        break
    tmp = A[i*K-1]
    if i!=1:
        t = A[(i-1)*K:i*K-1]
        t.sort(reverse=True)
        while t and -que[0] > t[-1]:
            S += que[0]
            heappop(que)
            S += t[-1]
            heappush(que,-t.pop())
    tmp += S
    ans = min(ans,tmp)
    S += A[i*K-1]
    heappush(que,-A[i*K-1])

print(ans)
0