You take exactly k cards, each one off the front or the back. That set straddles the two ends of the array, so it is not contiguous and no window can hold it. Its complement can: whatever you leave behind is one unbroken middle block of width exactly n − k. So stop solving the stated problem and solve the inverse — slide a fixed-width window of size n − k, find its minimum sum, and read the answer off as total − minSum. Maximising the ends and minimising the middle are the same instruction seen from opposite sides, which is why the tape below paints both at once. Its partner LC 1658 has the same complement trick with a variable-width middle.
total, one fixed-width slide.
Sub-variant H: the asked-for set is split across both ends, so you invert the question and window its
complement. answer = total − min(sum of any n−k contiguous cells), and when
k == n the window has width 0 and the answer is the whole total.