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()
zero,one = [],[]
cnt = 0
if s[0] == "0":
    flag = True
else:
    flag = False
for i in range(n-1):
    if s[i] == "0" and flag == True:
        cnt +=1
        if s[i] == "0" and s[i+1] =="1":
            zero.append(cnt)
            flag = False
            cnt = 0
    elif s[i] == "1" and flag == False:
        cnt +=1
        if s[i+1] =="0":
            one.append(cnt)
            flag = True
            cnt = 0
    else:
        cnt +=1
        
    if i == n-2:
        if s[i+1] == "0":
            cnt +=1
            zero.append(cnt)
        elif s[i+1] == "1":
            cnt +=1
            one.append(cnt)
            
sum1 = sum(zero[:k]) + sum(one[:k+1])
ans = sum1

if k >= len(zero):
    print(n)
    exit()
else:
    if s[0] == "1":
        for i in range(1,len(zero)-k):
            #print(sum1)
            sum1 -=(zero[i] + one[i])
            sum1 +=(zero[i+k] + one[i+k+1])
            ans = max(ans,sum1)
            #print(sum1,ans)
    elif s[0] =="0":
        print(sum1,ans)
        for i in range(1,len(zero)-k):
            sum1 -=(zero[i] + one[i])
            
            sum1 +=(zero[i+k] + one[i+k])
            ans = max(ans,sum1)
            
print(ans)

n,k = map(int,input().split())
s = input()
L = []
from itertools import groupby
for key ,value in groupby(s):
    L.append(list(value))
#print(L)
one_cnt = 0
zero_cnt = 0
zero_idx = 0
l,r = 0,0
ans = 0
while r <=len(L)-1:
    if L[r][0] == "1":
        one_cnt +=len(L[r])
        r +=1
    elif L[r][0] == "0":
        if zero_idx <k:
            zero_cnt +=len(L[r])
            r +=1
            zero_idx +=1
            
        elif zero_idx ==k:
            zero_cnt +=len(L[r][0])
            ans = max(ans,zero_cnt + one_cnt)
            while zero_idx == k:
                
                if l < len(L)-2:
                    l +=1
                elif l == len(L)-1:
                    break
                if L[l][0] == "1":
                    one_cnt -= len(L[l])
                    l += 1
                elif L[l][0] == "0":
                    zero_cnt -=len(L[l])
                    zero_idx -=1
    elif l == r:
        r +=1
print(ans)

n,k = map(int,input().split())
s = input()

L = []
from itertools import groupby
for key ,value in groupby(s):
    L.append((key,len(list(value))))
#print(L)
c = []
for i in range(len(L)):
    c.append(L[i][1])
c = [0] + c

for i in range(1,len(c)):
    c[i] +=c[i-1]
#print(c)
l = 0 
r = 2*k

ans = 0
while True:
    if L[l][0] == "1":
        if l + r +1 < len(L):
            ans = max(ans,c[l+r+1] - c[l])#add 5
        else:
            ans = max(ans,c[len(L)] - c[l])
    elif L[l][0] == "0":
        if l + r < len(L):
            ans = max(ans,c[l+r] - c[l])#add 4
        else:
            ans = max(ans,c[len(L)] - c[l])
    if l == len(L)-1:
        break
    l +=1
print(ans)

Verdicts: Runtime Error

Wrong Answer

Accepted