I don't remember the exact wording of the question, but it is clearly an activity selection problem, which is
[...] notable in that using a greedy algorithm to find a solution will always result in an optimal solution.
I put a rough description of the problem in the docstring, and present it together with my solution:
def solution(movies, K, L): """ Given two integers K and L, and a list movies where the value at the ith index represents the number of movies showing on the (i + 1)th day at the festival, returns the maximum number of movies that can be watched in two non-overlapping periods of K and L days, or -1 if no two such periods exist. """ kl_days = [ sum(movies[k_start:k_start + K] + movies[l_start:l_start + L]) for k_start in range(len(movies) - K) for l_start in range(k_start + K, len(movies) - L + 1) ] lk_days = [ sum(movies[k_start:k_start + K] + movies[l_start:l_start + L]) for l_start in range(len(movies) - L) for k_start in range(l_start + L, len(movies) - K + 1) ] days = [-1] + kl_days + lk_days return max(days)
Is there a more elegant way to solve this?
P.S. The function name is fixed for grading purposes.