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()