Maximum consecutive subarray sum for a variable sliding window size
While I'm not one who really promotes pasting solutions, a lot of the answers seem to be answering in a way that a C programmer would answer the question. This is more a problem that you would want to solve in a pipeline in python due to that being how python does things fast and keeps a majority of the operations in C. Look into David Beasley's work if you want to minimize the amount of code you have to write by making everything into pipelines and checkout the collections library to get to know the tools that make things like windowed operations easy and a lot faster in python.

from collections import deque
from itertools import chain

def window(iterable,size):
    ''' yields wondows of a given size '''
    d = deque(maxlen=size)
    # normalize iterable into a generator
    iterable = (i for i in iterable)
    # fill d until it only needs one more
    for i in iterable:
        d.append(i)
        if len(d) == size-1:
            break
    # yield the windows
    for i in iterable:
        d.append(i)
        yield d

def max_sequential_subarray_sum(iterable, max_size):
    ''' returns the max sum of sequential slices of an iterable '''
    g = chain(*(window(iterable,i) for i in range(2,max_size+1)))
    return max(sum(i) for i in g if all(
        a&lt;b for a,b in window(i,2)
    ) or all(
        a&gt;b for a,b in window(i,2)
    ))


L=[-200, 91, 82, 43] 
k=3
print(max_sequential_subarray_sum(L,k))

L=[41, 90, -62, -16, 25, -61, -33, -32, -33]
k=5
print(max_sequential_subarray_sum(L,k))