For "the largest sum of k elements in a row", adding up every window from scratch is wasted work. When the window slides one step only two elements change: one comes in, one goes out.
window = sum(values[:k])best = windowfor i in range(k, len(values)):window += values[i] - values[i - k]best = max(best, window)return best
The window does not have to be a fixed size. For "the shortest run whose sum is at least key" you grow the window on the right; while the condition holds you shrink it from the left and note the length each time. Both ends only ever move forward.