""" Rows of Stars First, use a sliding window to compute the sum of all windows of size s, call these values b. Then the sum over d days is b[i] + b[(i + k) % n] + b[(i + 2k) % n] + ... + b[(i + k d) % n] This breaks up b in to cycles, and we want sums over these cycles. We can compute these using a cycle finding algorithm in O(n) amortized time, but I chose to use a O(n log n) binary lifting algorithm instead. Finally, we need to know which indices we can start from. On any day < n - d, we can start from any of the first n - s indices of that day. We can use a difference array to mark which ones are possible. Overall runtime is O(n) or O(nlogn). """ def main(): n, k, d, s = map(int, input().split()) a = [int(input()) for _ in range(n)] b = [sum(a[:s])] for i in range(1, n): x = b[-1] - a[i - 1] + a[(i + s - 1) % n] b.append(x) # binary lifting on b b_sums = [b] sum_len = 2 while sum_len <= n: b_sums.append([]) for i in range(n): v = b_sums[-2][i] + b_sums[-2][(i + (sum_len // 2) * k) % n] b_sums[-1].append(v) sum_len *= 2 c = [] for i in range(n): v = 0 cur = i for j, b_sum in enumerate(b_sums): len = 1 << j if (d & len) > 0: v += b_sum[cur] cur = (cur + len * k) % n c.append(v) f = [0 for _ in range(n + 1)] for start_day in range(n - d + 1): start_star = start_day * k % n end_star = (start_star + n - s) % n if start_star <= end_star: f[start_star] += 1 f[end_star + 1] -= 1 else: f[start_star] += 1 f[0] += 1 f[end_star + 1] -= 1 valid = [] cumsum = 0 for i in range(n): cumsum += f[i] valid.append(cumsum > 0) ans = max(c[i] for i in range(n) if valid[i]) print(ans) if __name__ == "__main__": main()