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

n,k = map(int,input().split())
s = input()


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    ans = len(s)

elif s[0] == '0' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]
    
    ans = max(ans, anstmp)

    for i in range(1,zeros - k - 1):
        anstmp -= data[2*i + 1][1]
        anstmp -= data[2*i + 2][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i + 1][1]
    anstmp -= data[2*i + 2][1]
    anstmp += data[2*k + 2*i][1]

    
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]
    
    
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]
    
        
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
 
    
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]

    ans = max(ans, anstmp)
    
    for i in range(zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]

        ans = max(ans, anstmp)

if s[0] == '0' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]

    ans = max(ans, anstmp)
    
    for i in range(1, zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        ans = max(ans, anstmp)

print(ans)

import sys

n,k = map(int,input().split())
s = input()


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    print(len(s))
    sys.exit()    

if data[0][0] == '0':
    data.insert(0, (1,0))

if data[-1][0] == '0':
    data.append((1,0))

anstmp = 0
for i in range(2*k + 1):
    anstmp += data[i][1]

ans = max(ans, anstmp)

for i in range(zeros - k):
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i + 1][1]
    anstmp += data[2*k + 2*i + 2][1]

    ans = max(ans, anstmp)


print(ans)

Verdicts: Runtime Error

Accepted