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):
N,K=map(int,input().split())
S=input()+'E'

# Initial values
ans=-1
l=0
r=1
edge=[]



if S[l]=='0':
    count=1
else:
    count=0    


while(r < N+1):

    
    if (S[r] != S[r-1]) & (S[r]=='0'):
        count+=1

    
    while(count>K):
        l+=1
        
        if (S[l]!=S[l-1]) & (S[l]=='1'):
            count-=1
        if l==r:
            r+=1
            

    
    else:
        ans=max(ans,r-l)
        #print(r,l,count) 
        r+=1        
       

    
print(ans)

N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q[:(2*K+1)]:
    if S[p]==1:
        answers.append(q[min(2*K+1,N)]-p+1)
    else:
        answers.append(q[min(2*K,N)]-p+1)

N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for i,p in enumerate(q):
    if S[p]=='1':
        if i+2*K+1>len(q)-1:
                answers.append(N-p)
        else:
            answers.append(q[min(i+2*K+1,len(q)-1)]-p)
#    else:
#        answers.append(q[min(2*K,len(q)-1)]-p+1)
        
print(max(answers))

N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q[:(2*K+1)]:
    if S[p]==1:
        answers.append(q[min(2*K+1,N)]-p+1)
    else:
        answers.append(q[min(2*K,N)]-p+1)
        
print(max(answers)


N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for i,p in enumerate(q):
    if S[p]=='1':
        if i+2*K+1>=len(q)-1:
            answers.append(N-p)
        else:
            answers.append(q[min(i+2*K+1,len(q)-1)]-p)
    else:
        if i+2*K>=len(q)-1:
            answers.append(N-p)        
        answers.append(q[min(i+2*K,len(q)-1)]-p)
        
print(max(answers))

N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q:
    if S[p]=='1':
        answers.append(q[min(2*K+1,len(q)-1)]-p+1)
    else:
        answers.append(q[min(2*K,len(q)-1)]-p+1)
        
print(max(answers))

N,K=map(int,input().split())
S=input()+'E'

# Initial values
ans=-1
l=0
r=1



for l in range(N):

    
    
    if S[l]=='0':
        count=1
    else:
        count=0    
    
    
    for r in range(l+1,N+1):

        
        if (S[r] != S[r-1]) & (S[r]=='0'):
            count+=1

            
            if count>K:

                
                #if (S[l]!=S[l-1]) & (S[i]=='1'):
                #    count-=1
                break

        
        #print(r,l)
        ans=max(ans,r-l)

print(ans)

ans=-1
l=0

r=0
R=0

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)
q.append(N+1)
        
L=q[l]
R=q[r]

S[L:R]

for l in range(len(q)):
    count=0
    for r in range(l+1,len(q)):        
        L=q[l]
        R=q[r]
        print(S[L:R])
        if S[L:R][-1]=='0':
            count+=1
            if count>K:
                break
        ans=max(ans,len(S[L:R]))
        
print(ans)        

N,K=map(int,input().split())
S=input()

ans=-1
l=0

r=0
R=0

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)
q.append(N+1)
        
L=q[l]
R=q[r]

S[L:R]

for l in range(len(q)):
    count=0
    for r in range(l+1,len(q)):        
        L=q[l]
        R=q[r]
        if S[L:R][-1]=='0':
            count+=1
            if count>K:
                break
        ans=max(ans,len(S[L:R]))
        
print(ans)

N,K=map(int,input().split())
S=input()+'E'

# Initial values
ans=1
l=0
r=0
edge=[]



if S[l]=='0':
    count=1
else:
    count=0    


while(r < N-1):
    r+=1
    
    if ( S[r-1]=='1') & (S[r]=='0'):
        count+=1

    
    while(count>K):
        l+=1
        
        if (S[l-1]=='0') & (S[l]=='1'):
            count-=1
        
        if l==r:
            r+=1
        print(r,l,count,ans)

    
    ans=max(ans,r-l+1)
    print(r,l,count,ans) 

print(ans)

N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for i,p in enumerate(q):
    if S[p]=='1':
        if i+2*K+1>len(q)-1:
            answers.append(N-p)
        else:
            answers.append(q[min(i+2*K+1,len(q)-1)]-p)
    else:
        answers.append(q[min(2*K,len(q)-1)]-p)
        
print(max(answers))

N,K=map(int,input().split())
S=input()+'E'

# Initial values
ans=-1
l=0
r=1
edge=[]



if S[l]=='0':
    count=1
else:
    count=0    


while(r < N):

    
    if (S[r] != S[r-1]) & (S[r]=='0'):
        count+=1

    
    while(count>K):
        l+=1
        
        if (S[l]!=S[l-1]) & (S[l]=='1'):
            count-=1
        if l==r:
            r+=1
        print(r,l,count)     

    
    ans=max(ans,r-l+1)
    #print(r,l,count,ans) 
    r+=1        
       

    
print(ans)

N,K=map(int,input().split())
S=input()

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q[:(2*K+1)]:
    if S[p]==1:
        answers.append(q[min(2*K+1,N)]-p+1)
    else:
        answers.append(q[min(2*K,N)]-p+1)
        
print(max(answers))

N,K=map(int,input().split())
S=input()+'E'

# Initial values
ans=1
l=0
r=0
edge=[]



if S[l]=='0':
    count=1
else:
    count=0    


while(r < N-1):
    r+=1
    
    if ( S[r-1]=='1') & (S[r]=='0'):
        count+=1

    
    while(count>K):
        l+=1
        
        if (S[l-1]=='0') & (S[l]=='1'):
            count-=1
        
        if l==r:
            r+=1
        #print(r,l,count,ans)

    
    ans=max(ans,r-l+1)
    #print(r,l,count,ans) 

print(ans)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted