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

icase=0
if icase==0:
    n,k=map(int,input().split())
    s=input()
elif icase==1:
    n,k=14,2
    s="11101010110011"
elif icase==2:
    n,k=15,2
    s="111010101100110"
elif icase==3:
    n,k=16,2
    s="1110101011001101"
    s="1110101011001100"

if n==1:
    print(1)
    sys.exit()

ss=[]
if s[0]=="1" and s[1]=="1":
    zcnt=1
elif s[0]=="1" and s[1]=="0":
    ss.append(1)
    zcnt=0
elif s[0]=="0" and s[1]=="0":
    ss.append(0)
    zcnt=1
elif s[0]=="0" and s[1]=="1":
    ss.append(0)
    ss.append(1)
    zcnt=1

for i in range(1,n-1):
    if s[i]=="0" and s[i-1]=="0":
        zcnt+=1
    elif s[i]=="0" and s[i-1]=="1":
        ss.append(zcnt)
        zcnt=1
    elif s[i]=="1" and s[i-1]=="1":
        zcnt+=1
    elif s[i]=="1" and s[i-1]=="0":
        ss.append(zcnt)
        zcnt=1
        
for i in range(n-1,n):
    if s[i]=="0" and s[i-1]=="0":
        zcnt+=1
        ss.append(zcnt)
        ss.append(0)
    elif s[i]=="0" and s[i-1]=="1":
        ss.append(zcnt)
        ss.append(1)
        ss.append(0)
    elif s[i]=="1" and s[i-1]=="1":
        zcnt+=1
        ss.append(zcnt)
    elif s[i]=="1" and s[i-1]=="0":
        ss.append(zcnt)
        ss.append(1)

print(ss)

if len(ss)<=2*k+1:
    print(sum(ss))
    sys.exit()

smax=0
for i in range(2*k+1):
    smax+=ss[i]
print(smax)    
ssum=smax    
kk=2*k
for i in range(2,len(ss)-kk,2):
    ssum=ssum-ss[i-2]-ss[i-1]+ss[i-2+kk]+ss[i-1+kk+1]
    smax=max(smax,ssum)
    print(i,ssum)

print(smax)
  

import sys

icase=0
if icase==0:
    n,k=map(int,input().split())
    s=input()

if n==1:
    print(1)
    sys.exit()

ss=[]
if s[0]=="1" and s[1]=="1":
    zcnt=1
elif s[0]=="1" and s[1]=="0":
#    ss.append(1)
    zcnt=1
elif s[0]=="0" and s[1]=="0":
    ss.append(0)
    zcnt=1
elif s[0]=="0" and s[1]=="1":
    ss.append(0)
#    ss.append(1)
    zcnt=1

for i in range(1,n-1):
    if s[i]=="0" and s[i-1]=="0":
        zcnt+=1
    elif s[i]=="0" and s[i-1]=="1":
        ss.append(zcnt)
        zcnt=1
    elif s[i]=="1" and s[i-1]=="1":
        zcnt+=1
    elif s[i]=="1" and s[i-1]=="0":
        ss.append(zcnt)
        zcnt=1
        
for i in range(n-1,n):
    if s[i]=="0" and s[i-1]=="0":
        zcnt+=1
        ss.append(zcnt)
        ss.append(0)
    elif s[i]=="0" and s[i-1]=="1":
        ss.append(zcnt)
        ss.append(1)
        ss.append(0)
    elif s[i]=="1" and s[i-1]=="1":
        zcnt+=1
        ss.append(zcnt)
    elif s[i]=="1" and s[i-1]=="0":
        ss.append(zcnt)
        ss.append(1)

#print(ss)

if len(ss)<=2*k+1:
    print(n)
    sys.exit()

smax=0
for i in range(2*k+1):
    smax+=ss[i]
#print(smax)    
ssum=smax    
kk=2*k
for i in range(2,len(ss)-kk,2):
    ssum=ssum-ss[i-2]-ss[i-1]+ss[i+kk-1]+ss[i+kk]
    smax=max(smax,ssum)

print(smax)
    

Verdicts: Wrong Answer

Accepted