結果

問題 No.2139 K Consecutive Sushi
ユーザー ygd.ygd.
提出日時 2022-12-05 22:20:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 615 ms
コンパイル使用メモリ 82,392 KB
実行使用メモリ 106,548 KB
最終ジャッジ日時 2024-04-20 20:45:22
合計ジャッジ時間 5,904 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,720 KB
testcase_01 AC 40 ms
53,932 KB
testcase_02 AC 39 ms
55,000 KB
testcase_03 AC 240 ms
106,064 KB
testcase_04 AC 243 ms
106,044 KB
testcase_05 AC 233 ms
106,292 KB
testcase_06 AC 229 ms
106,548 KB
testcase_07 AC 245 ms
105,756 KB
testcase_08 AC 206 ms
105,916 KB
testcase_09 AC 211 ms
106,524 KB
testcase_10 AC 223 ms
105,784 KB
testcase_11 AC 225 ms
106,292 KB
testcase_12 AC 213 ms
105,904 KB
testcase_13 AC 39 ms
55,160 KB
testcase_14 AC 39 ms
55,196 KB
testcase_15 AC 39 ms
55,572 KB
testcase_16 AC 39 ms
54,680 KB
testcase_17 AC 40 ms
55,104 KB
testcase_18 AC 78 ms
77,244 KB
testcase_19 AC 85 ms
76,800 KB
testcase_20 AC 84 ms
76,800 KB
testcase_21 AC 46 ms
59,264 KB
testcase_22 AC 92 ms
76,976 KB
testcase_23 AC 118 ms
78,336 KB
testcase_24 AC 140 ms
85,872 KB
testcase_25 AC 201 ms
103,092 KB
testcase_26 AC 108 ms
78,712 KB
testcase_27 AC 141 ms
84,608 KB
testcase_28 AC 132 ms
85,040 KB
testcase_29 AC 123 ms
80,384 KB
testcase_30 AC 149 ms
88,624 KB
testcase_31 AC 235 ms
103,176 KB
testcase_32 AC 112 ms
80,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
import math
import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
from collections import defaultdict
from collections import deque
#import copy #DeepCopy: hoge = [_[:] for _ in hogehoge]
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r): #これで[l,r)から取り出す。
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)


def main():
    N,K = map(int,input().split()); INF = 1<<60
    A = list(map(int,input().split()))
    S = sum(A)
    # dp[i]: i番目まで見てi番目を食べないとき食べないものの総和
    dp = SegmentTree([INF]*(N+1), min, INF)
    dp.update(0,0)
    for i in range(1,N+1):
        dp.update(i,dp.sum(max(i-K,0),i) + A[i-1])
        # print(dp[i])
    
    ans = S - dp.sum(N-K+1,N+1)
    print(ans)

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