# Title: Sliding window
# Heading: Python Program to Find the Maximum Sum of K Consecutive Elements
# Level: intermediate
# Tags: sliding window, maximum subarray sum, k consecutive elements, window
# Time: O(n)
# Space: O(1)
# About: Finds the largest sum of k items in a row by sliding a window along the list, adding the item that enters and taking away the one that leaves.

def max_window_sum(items, k):
    """The largest sum of k consecutive items."""
    window = sum(items[i] for i in range(k))
    best = window
    for i in range(k, len(items)):
        # Add the new item, take away the one that left
        window += items[i] - items[i - k]
        best = max(best, window)
    return best


sales = [5, 2, 8, 4, 9, 1, 3, 6]  # one number per day
print("Best 3-day total:", max_window_sum(sales, 3))
