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):

# coding: utf-8

# In[24]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[26]:

tmp = S[0]
c_list = []
counter = 1
for i in range(1, len(S)):
    #counter += 1
    #print(S[i], i)
    if S[i] != tmp:
        c_list.append(counter)
        counter = 0
        tmp = S[i]
    counter += 1
    if i == len(S)-1:
        c_list.append(counter)
    #print(S[i], i,counter, c_list)

#print(c_list)
pl = [0]*len(c_list)
if len(c_list) <= K*2:
    print(len(S))
else:
    for i in range(len(c_list)-K*2):
        pl[i] = sum(c_list[i:i+K*2+1])
    #print(pl)
    print(max(pl))


# In[ ]:






# coding: utf-8

# In[1]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[2]:


#print(len(S), S)


# In[4]:

tmp = S[0]
sums = [0]*(N+1)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+1]
pl = [0]*gc
if gc < K*2:
    print(N)
else:
    if S[0] == "0":
        for i in range(-1, gc-K*2+1, 2):
            j = 0 if i < 0 else i
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        for i in range(0, gc-K*2+1, 2):
            j = i
            pl[i] = sums[i+1+K*2]-sums[j]
    print(max(pl))


# In[ ]:






# coding: utf-8

# In[5]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[6]:


#print(len(S), S)


# In[21]:

tmp = S[0]
sums = [0]*(N+2)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+2]
#print(S)
#print(sums)
#print(gc)

pl = [0]*(gc+2)
if gc < K*2:
    print(N)
else:
    if S[0] == "0":
        if S[-1] == "0":
            for i in range(-1, gc-K*2+1, 2):
                #print(i, i+1+K*2, gc)
                j = 0 if i < 0 else i
                l = gc if i+1+K*2>gc else i+1+K*2
                #print(j, l)
                pl[i] = sums[l]-sums[j]
        else:
            for i in range(-1, gc-K*2, 2):
                j = 0 if i < 0 else i
                pl[i] = sums[i+1+K*2]-sums[j]
    else:
        if S[-1] == "0":
            for i in range(0, gc-K*2+1, 2):
                #print(i, i+1+K*2, gc)
                j=i
                l = gc if i+1+K*2>gc else i+1+K*2
                #print(j, l)
                pl[i] = sums[l]-sums[j]
        else:
            for i in range(0, gc-K*2, 2):
                j = i
                pl[i] = sums[i+1+K*2]-sums[j]
    #print(pl)
    print(max(pl))


# In[ ]:





Verdicts: Wrong Answer

Runtime Error

Accepted