------
## 最终结论
```python
def longest_ones(A, K):
    left = 0  # Initialize the left boundary of the window
    count_zero = 0  # Counter for the number of zeros in the current window
    max_length = 0  # Variable to store the maximum length of subarray

    for right in range(len(A)):  # Traverse through the array using the right boundary of the window
        if A[right] == 0:  # If we encounter a zero
            count_zero += 1  # Increment the zero counter

        # If the number of zeros exceeds K, move the left boundary of the window
        while count_zero > K:
            if A[left] == 0:  # If the leftmost element is zero
                count_zero -= 1  # Decrement the zero counter
            left += 1  # Move the left boundary to the right

        # Calculate the maximum length of the window after adjusting the left boundary
        max_length = max(max_length, right - left + 1)  # Update the maximum length

    return max_length
```