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):
def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)
    
N,K=map(int,input().split())
S=input()

RL=ranlength(S)
#print(RL)


ans1,ans2=0,0
if int(S[0])==1:
    cnt1=RL[0]
    ans1=RL[0]
    for i in range(len(RL)):
        if i%2==0 and i>=2:
            cnt1+=RL[i]
            cnt1+=RL[i-1]
            if i>=K*2+2:
                cnt1-=RL[i-(K*2+1)]
                cnt1-=RL[i-(K*2+2)]
            ans1=max(ans1,cnt1)
            #print(cnt1)
    #print(ans1)
elif int(S[0])==0:
    if len(RL)==1:
        ans2=RL[0]
    else:
        cnt2=RL[0]+RL[1]
        ans2=RL[0]+RL[1]
    for i in range(len(RL)):
        if i%2==1 and i>=3:
            cnt2+=RL[i]
            cnt2+=RL[i-1]
            if i==K*2+1:
                cnt2-=RL[0]
            elif i>=K*2+3:
                cnt2-=RL[i-(K*2+1)]
                cnt2-=RL[i-(K*2+2)]
            ans2=max(ans2,cnt2)
            #print(cnt2)            

RL=RL[::-1]
ans3,ans4=0,0
if int(S[0])==1:
    cnt1=RL[0]
    ans1=RL[0]
    for i in range(len(RL)):
        if i%2==0 and i>=2:
            cnt1+=RL[i]
            cnt1+=RL[i-1]
            if i>=K*2+2:
                cnt1-=RL[i-(K*2+1)]
                cnt1-=RL[i-(K*2+2)]
            ans3=max(ans3,cnt1)
            #print(cnt1)
    #print(ans3)
elif int(S[0])==0:
    if len(RL)==1:
        ans4=RL[0]
    else:
        cnt2=RL[0]+RL[1]
        ans2=RL[0]+RL[1]
    for i in range(len(RL)):
        if i%2==1 and i>=3:
            cnt2+=RL[i]
            cnt2+=RL[i-1]
            if i==K*2+1:
                cnt2-=RL[0]
            elif i>=K*2+3:
                cnt2-=RL[i-(K*2+1)]
                cnt2-=RL[i-(K*2+2)]
            ans4=max(ans4,cnt2)
            #print(cnt2)            

print(max([ans1,ans2,ans3,ans4]))


def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)
    
N,K=map(int,input().split())
S=input()

RL=ranlength(S)
if S[0]=='0':
    RL=[0]+RL
if S[-1]=='0':
    RL.append(0)


cnt=RL[0]
ans=RL[0]
for i in range(len(RL)):
    if i%2==0 and i>=2:
        cnt+=RL[i]
        cnt+=RL[i-1]
        if i>=K*2+2:
            cnt-=RL[i-(K*2+1)]
            cnt-=RL[i-(K*2+2)]
        ans=max(ans,cnt)
        #print(cnt)
print(ans)

Verdicts: Wrong Answer

Accepted