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):
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = '1' + read()
sum_0 = [0]

for s_new, s_next in zip(S[:-2], S[1:-1]):
    # print(s_new, s_next)
    if s_new == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
    else:
        sum_0.append(sum_0[-1])

S = S[1:-1]
# print(sum_0)

left = 0  
ans = 1  
right = 1



# print(sum_0)

for left in range(N-1):
    while (True):
        right += 1
        if sum_0[right + 1] - sum_0[left] > K:
            right -= 1
            break
        if right == N - 1:
            break
        # print(left, right, sum_0[right + 1] - sum_0[left])

    # print(left, right)
    ans = max(ans, right - left+1)  
    # print(ans)

print(ans)


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = '1'+read()

sum_0 = [0]

for s, s_next in zip(S[:-2], S[1:-1]):
    if s == s_next:
        sum_0.append(sum_0[-1])
        continue

    if s == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
        continue

    sum_0.append(sum_0[-1])


def get_1length(left, right):
    return len()


left = 0  
ans = 1  
right = 1



print(sum_0)

for left in range(N+1):
    # if sum_0[right] - sum_0[left] > K:
    #     continue

    while sum_0[right] - sum_0[left] <= K:
        if right == N:
            break
        right += 1

    # print(left, right)
    ans = max(ans, right - left)  
    # print(ans)

print(ans)


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = '1'+read()

sum_0 = [0]

for s, s_next in zip(S[:-2], S[1:-1]):
    if s == s_next:
        sum_0.append(sum_0[-1])
        continue

    if s == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
        continue

    sum_0.append(sum_0[-1])






left = 0  
ans = 1  
right = 1



# print(sum_0)

for left in range(N+1):
    # if sum_0[right] - sum_0[left] > K:
    #     continue

    while sum_0[right] - sum_0[left] <= K:
        if right == N:
            break
        right += 1

    # print(left, right)
    ans = max(ans, right - left)  
    # print(ans)

print(ans)


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = read()

if S[0] == '0':
    sum_0 = [0, 1]
else:
    sum_0 = [0, 0]


for s, s_next in zip(S[:-2], S[1:-1]):
    if s == s_next:
        sum_0.append(sum_0[-1])
        continue

    if s == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
        continue
    sum_0.append(sum_0[-1])






left = 0  
ans = 1  
right = 1



# print(sum_0)

for left in range(N):
    # if sum_0[right] - sum_0[left] > K:
    #     continue

    while sum_0[right] - sum_0[left] <= K:
        if right == N:
            break
        right += 1

    # print(left, right)
    ans = max(ans, right - left)  
    # print(ans)

print(ans)





import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = read()[:-1]
sum_0 = [0]



now = '1'
for s_next in S:
    if s_next == '0' and now == '1':
        sum_0.append(sum_0[-1] + 1)
    else:
        sum_0.append(sum_0[-1])
    now = s_next

sum_0_2 = [0]



for s_now, s_next in zip(S[:-1], S[1:]):
    if s_next == '1' and s_now == '0':
        sum_0_2.append(sum_0_2[-1] + 1)
    else:
        sum_0_2.append(sum_0_2[-1])

if S[-1] == '0':
    sum_0_2.append(sum_0_2[-1] + 1)
else:
    sum_0_2.append(sum_0_2[-1])

# print(S)
# print(sum_0)
# print(sum_0_2)


left = 0
right = 1
ans = 1  



# print(sum_0)

for left in range(N - 1):
    while right < N:
        nextright = right+1
        
        if sum_0[nextright] - sum_0_2[left] > K:
            break
        right += 1
    #print(right, right < N)
    ans = max(ans, right - left)  
    #print(left, right, ans, sum_0[nextright] - sum_0_2[left])

print(ans)


Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted