from collections import deque import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def solve(): cs = [0] for a in aa: cs.append(cs[-1] + a) dp = [-inf] * (n + 1) dp[0] = 0 for j in range(k): q1 = deque() q2 = deque() ndp = [-inf] * (n + 1) for i in range(n + 1): while q1 and q1[0][0] < i - m: q1.popleft() while q2 and q2[0][0] < i - m: q2.popleft() if q1: ndp[i] = max(q1[0][1] + cs[i], q2[0][1] - cs[i]) if dp[i] == -inf: continue val = dp[i] - cs[i] while q1 and q1[-1][1] <= val: q1.pop() q1.append((i, val)) val = dp[i] + cs[i] while q2 and q2[-1][1] <= val: q2.pop() q2.append((i, val)) # print(q1,q2) dp = ndp print(dp[-1]) inf=10**16 n,k,m=MI() aa=LI() solve()