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=list(input())
st=[]
en=[]
if s[0]=="1":
    st.append(0)
for i in range(1,n):
    if s[i-1]=="0" and s[i]=="1":
        st.append(i)
    if s[i-1]=="1" and s[i]=="0":
        en.append(i)
if s[n-1]=="1":
    en.append(n)
ans=0
if len(en)+1<=k:
    print(n)
    exit()
if len(en)<=k and s[0]=="1":
    print(n)
    exit()
if len(en)<=k and s[n-1]=="1":
    print(n)
    exit()
if len(en)-1<=k and s[n-1]=="1" and s[0]=="1":
    print(n)
    exit()
    
if len(en)<=k:
    k=len(en)
for i in range(len(en)-k):
    ans=max(ans,en[i+k]-st[i])
if s[0]=="0":
    ans=max(ans,en[k-1])
if s[n-1]=="0":
    ans=max(ans,n-st[len(st)-k])
print(ans,st,en)


n,k=map(int,input().split())
s=list(input())
st=[]
en=[]
if s[0]=="1":
    st.append(0)
for i in range(1,n):
    if s[i-1]=="0" and s[i]=="1":
        st.append(i)
    if s[i-1]=="1" and s[i]=="0":
        en.append(i)
if s[n-1]=="1":
    en.append(n)
ans=0
if len(en)+1<=k:
    print(n)
    exit()
if len(en)<=k and s[0]=="1":
    print(n)
    exit()
if len(en)<=k and s[n-1]=="1":
    print(n)
    exit()
if len(en)-1<=k and s[n-1]=="1" and s[0]=="1":
    print(n)
    exit()
    
if len(en)<=k:
    k=len(en)
for i in range(len(en)-k):
    ans=max(ans,en[i+k]-st[i])
if s[0]=="0":
    ans=max(ans,en[k-1])
if s[n-1]=="0":
    ans=max(ans,n-st[len(st)-k]+1)
print(ans,st,en)


n,k=map(int,input().split())
s=list(input())
st=[]
en=[]
if s[0]=="1":
    st.append(0)
for i in range(1,n):
    if s[i-1]=="0" and s[i]=="1":
        st.append(i)
    if s[i-1]=="1" and s[i]=="0":
        en.append(i)
if s[n-1]=="1":
    en.append(n)
ans=0
if len(en)+1<=k:
    print(n)
    exit()
if len(en)<=k and s[0]=="1":
    print(n)
    exit()
if len(en)<=k and s[n-1]=="1":
    print(n)
    exit()
if len(en)-1<=k and s[n-1]=="1" and s[0]=="1":
    print(n)
    exit()
    
if len(en)<=k:
    k=len(en)
for i in range(len(en)-k):
    ans=max(ans,en[i+k]-st[i]+1)
if s[0]=="0":
    ans=max(ans,en[k-1])
if s[n-1]=="0":
    ans=max(ans,n-st[len(st)-k]+1)
print(ans,st,en)


n,k=map(int,input().split())
s=list(input())
st=[]
en=[]
if s[0]=="1":
    st.append(0)
for i in range(1,n):
    if s[i-1]=="0" and s[i]=="1":
        st.append(i)
    if s[i-1]=="1" and s[i]=="0":
        en.append(i)
if s[n-1]=="1":
    en.append(n)
ans=0
if len(en)+1<=k:
    print(n)
    exit()
if len(en)<=k and s[0]=="1":
    print(n)
    exit()
if len(en)<=k and s[n-1]=="1":
    print(n)
    exit()
if len(en)-1<=k and s[n-1]=="1" and s[0]=="1":
    print(n)
    exit()
    
if len(en)<=k:
    k=len(en)
for i in range(len(en)-k):
    ans=max(ans,en[i+k]-st[i])
if s[0]=="0":
    ans=max(ans,en[k-1])
if s[n-1]=="0":
    ans=max(ans,n-st[len(st)-k])
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted