import sys from collections import deque def main(): N, K, M = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) S = [0] * (N + 1) for i in range(1, N+1): S[i] = S[i-1] + A[i-1] INF = -float('inf') dp = [ [INF]*(N+1) for _ in range(K+1) ] dp[0][0] = 0 for j in range(1, K+1): dq1 = deque() # Stores tuples of (i', value) where value is dp[j-1][i'] - S[i'] dq2 = deque() # Stores tuples of (i', value) where value is dp[j-1][i'] + S[i'] for i in range(j, N+1): start_i = max(j-1, i - M) # Check if i-1 is a valid i' for the previous layer if i-1 >= 0 and dp[j-1][i-1] != INF: current_val1 = dp[j-1][i-1] - S[i-1] # Maintain deque1 in descending order while dq1 and dq1[-1][1] <= current_val1: dq1.pop() dq1.append((i-1, current_val1)) current_val2 = dp[j-1][i-1] + S[i-1] while dq2 and dq2[-1][1] <= current_val2: dq2.pop() dq2.append((i-1, current_val2)) # Remove elements from the front that are out of the window while dq1 and dq1[0][0] < start_i: dq1.popleft() while dq2 and dq2[0][0] < start_i: dq2.popleft() max_val1 = dq1[0][1] if dq1 else INF max_val2 = dq2[0][1] if dq2 else INF current_max = max(max_val1 + S[i], max_val2 - S[i]) if current_max != INF: dp[j][i] = current_max else: dp[j][i] = INF print(dp[K][N]) if __name__ == '__main__': main()