結果

問題 No.2139 K Consecutive Sushi
ユーザー tktk_snsntktk_snsn
提出日時 2022-12-03 08:18:35
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,314 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 109,224 KB
最終ジャッジ日時 2024-04-18 16:38:02
合計ジャッジ時間 7,230 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,224 KB
testcase_01 AC 41 ms
52,736 KB
testcase_02 AC 43 ms
52,096 KB
testcase_03 AC 292 ms
106,200 KB
testcase_04 AC 312 ms
106,216 KB
testcase_05 AC 296 ms
105,828 KB
testcase_06 AC 288 ms
106,212 KB
testcase_07 AC 299 ms
106,080 KB
testcase_08 AC 270 ms
106,164 KB
testcase_09 AC 267 ms
106,208 KB
testcase_10 AC 280 ms
105,688 KB
testcase_11 AC 269 ms
105,888 KB
testcase_12 AC 276 ms
105,932 KB
testcase_13 AC 37 ms
52,096 KB
testcase_14 AC 39 ms
52,352 KB
testcase_15 AC 37 ms
52,352 KB
testcase_16 WA -
testcase_17 AC 37 ms
52,480 KB
testcase_18 AC 82 ms
77,056 KB
testcase_19 AC 86 ms
76,800 KB
testcase_20 AC 84 ms
76,416 KB
testcase_21 AC 46 ms
59,392 KB
testcase_22 AC 110 ms
77,112 KB
testcase_23 AC 126 ms
78,952 KB
testcase_24 AC 175 ms
85,632 KB
testcase_25 AC 238 ms
102,400 KB
testcase_26 AC 124 ms
78,396 KB
testcase_27 AC 166 ms
84,204 KB
testcase_28 AC 167 ms
84,096 KB
testcase_29 AC 137 ms
79,764 KB
testcase_30 AC 176 ms
88,320 KB
testcase_31 AC 282 ms
109,224 KB
testcase_32 AC 139 ms
80,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree(object):
    def __init__(self, N, op_data, u_data):
        self._n = N
        self.log = (N-1).bit_length()
        self.size = 1 << self.log

        self.op = op_data
        self.e = u_data

        self.data = [u_data] * (2 * self.size)
        # self.len = [1] * (2 * self.size)

    def __repr__(self):
        res = [self.get(i) for i in range(self._n)]
        return " ".join(map(str, res))

    def _update(self, i):
        self.data[i] = self.op(self.data[i << 1], self.data[i << 1 | 1])

    def initialize(self, arr=None):
        """ segtreeをarrで初期化する。len(arr) == Nにすること """
        if arr:
            for i, a in enumerate(arr, self.size):
                self.data[i] = a
        for i in reversed(range(1, self.size)):
            self._update(i)
            # self.len[i] = self.len[i << 1] + self.len[i << 1 | 1]

    def update(self, p, x):
        """ data[p] = x とする (0-indexed)"""
        p += self.size
        self.data[p] = max(x, self.data[p])
        for i in range(1, self.log + 1):
            self._update(p >> i)

    def get(self, p):
        """ data[p]を返す """
        return self.data[p + self.size]

    def prod(self, l, r):
        """
        op_data(data[l], data[l+1], ..., data[r-1])を返す (0-indexed)
        """
        sml = self.e
        smr = self.e
        l += self.size
        r += self.size

        while l < r:
            if l & 1:
                sml = self.op(sml, self.data[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self.op(self.data[r], smr)
            l >>= 1
            r >>= 1
        return self.op(sml, smr)

    def all_prod(self):
        """ op(data[0], data[1], ... data[N-1])を返す """
        return self.data[1]


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    S = [0] * (N + 3)
    for i, a in enumerate(A, 1):
        S[i] = S[i-1] + a

    seg = SegTree(N + 3, max, -10**18)
    seg.update(0, 0)
    ans = 0
    for i in range(1, N + 1):
        L = max(0, i-K+1)
        dp = seg.prod(L, i) + S[i]
        ans = max(ans, dp)
        if i + 1 <= N:
            seg.update(i+1, dp - S[i+1])
        if i+2 <= N:
            seg.update(i+2, dp - S[i+2])

    print(ans)


main()
0