import sys import math from collections import defaultdict def main(): N, M, L = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) # Precompute sum_r: sum of A_p where p mod L = r sum_r = defaultdict(int) for p in range(1, N+1): r = p % L sum_r[r] += A[p-1] g = math.gcd(N, L) T = L // g D = (-N) % L # Step in the arithmetic progression max_sum = -float('inf') # We need to check all residues modulo L, but this is impossible for large L. # So this code will not work for large L, but here for demonstration. # However, given the problem constraints, this approach is not feasible. # The correct approach involves mathematical insights beyond the current scope. # For the sample input, we can compute the sum for each i in 1..L and find the maximum. # But for large L, this is impossible. # The following code is for demonstration and will not pass large test cases. # Compute for each i in 1..L for i in range(1, L+1): total = 0 # Compute k from 0 to M-1: (i - k*N) mod L # This is equivalent to (i + k*D) mod L # But with M up to 1e9, we need to find the sum using periodicity full_cycles = M // T remaining = M % T # Compute sum for full cycles full_sum = 0 current = i % L seen = dict() cycle_values = [] for k in range(T): if current in seen: break seen[current] = k cycle_values.append(current) current = (current + D) % L # Now, cycle_values contains the residues for one full cycle full_sum = sum(sum_r[r] for r in cycle_values) total += full_cycles * full_sum # Compute sum for remaining current = i % L for k in range(remaining): total += sum_r.get(current, 0) current = (current + D) % L if total > max_sum: max_sum = total print(max_sum) if __name__ == "__main__": main()