結果

問題 No.2139 K Consecutive Sushi
コンテスト
ユーザー rlangevin
提出日時 2023-10-13 12:25:06
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 1,122 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-10-01 01:11:49
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

class SegmentTree:
    def __init__(self, size, f=min, default=10**18):
        self.size = 2**(size-1).bit_length() 
        self.default = default
        self.dat = [default]*(self.size*2) 
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) 
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res
    

N, K = map(int, input().split())
A = list(map(int, input().split()))
T = SegmentTree(N + 1)
T.update(0, 0)
ans = sum(A)
for i in range(N):
    v = T.query(max(0, i - K + 1), i + 1)
    T.update(i + 1, v + A[i])

print(ans - T.query(max(0, N - K + 1), N + 1))
0