Problem: N people are arranged in a row from left to right.  You are given a string S of length N consisting of `0` and `1`, and a positive integer K.  The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`.  You will give the following direction at most K times (possibly zero):  **Direction** : Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.  Find the maximum possible number of **consecutive** people standing on hands after at most K directions.

Submission attempts (in order):
from itertools import groupby
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, K = tuple(li())
S = stdin.readline() + "2"
# use window
l, r = 0, 0
cnt = 0
r_iter = groupby(S)
l_iter = groupby(S)
r_next = next(r_iter)
l_next = next(l_iter)
while r_next[0] != "2":
    # print(l, r)
    if r_next[0] == '1' and K >= 0:
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
        K -= 1
        continue
    elif K >=0:
        if r_next[0] == "0":
            pass
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        continue
    if l_next[0] == "0":
        K += 1
    elif l_next[0] == "1":
        pass
    l += len(list(l_next[1]))
    l_next = next(l_iter)
cnt = max(cnt, r-l)
print(cnt)


from itertools import groupby
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, K = tuple(li())
S = stdin.readline() + "2"
# use window
l, r = 0, 0
cnt = 0
r_iter = groupby(S)
l_iter = groupby(S)
r_next = next(r_iter)
l_next = next(l_iter)
while r < N:
    
    if K > 0:
        if r_next[0] == '1' and r > 0:
            K -= 1
        cnt = max(cnt, r - l)
        r += len(list(r_next[1]))
        r_next = next(r_iter)
    elif K == 0 and r_next[0] == "1":
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
    else:
        if l_next[0] == "0":
            K += 1
        l += len(list(l_next[1]))
        l_next = next(l_iter)

if S[N-1] == '1':
    cnt = max(cnt, r-l)
print(cnt)



from itertools import groupby
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, K = tuple(li())
S = stdin.readline() + "2"
# use window
l, r = 0, 0
cnt = 0
r_iter = groupby(S)
l_iter = groupby(S)
r_next = next(r_iter)
l_next = next(l_iter)
while r < N:
    
    if K > 0:
        if r_next[0] == '1' and r > 0:
            K -= 1
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
    elif K == 0 and r_next[0] == "1":
        cnt = max(cnt, r - l)
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
    else:
        if l_next[0] == "0":
            K += 1
        l += len(list(l_next[1]))
        l_next = next(l_iter)

if r_next[0] == '1':
    cnt = max(cnt, r-l)
print(cnt)


Verdicts: Wrong Answer

Wrong Answer

Accepted