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()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            K = min(K,len(C)//A+1)
            ans = accumulated[i+2*K] - accumulated[i]
        else:
            K = min(K,len(C)//2)
            ans = accumulated[i+2*K+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)
　

N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            K = min(K,len(C)//A+1)
            ans = accumulated[i+2*K] - accumulated[i]
        else:
            K = min(K,len(C)//2)
            ans = accumulated[i+2*K+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)


N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
print(accumulated)
if len(C) == 1:
    ans = C[0]
elif len(C) == 2:
    ans = C[0] + C[2]

if flag == 1:
    try:
        for i in range(len(C)):
            if i % 2 == 0:
                cand = accumulated[2*K+1+i] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass
else:
    try:
        for i in range(len(C)):
            if i % 2 == 1:
                cand = accumulated[2*K+1+i] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass

print(ans)





N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
if flag == 0:
    if len(C) % 2 == 0:
        k = min(K,len(C)//2)
    else:
        k = min(K,len(C)//2+1)
else:
    k = min(K,len(C)//2)
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            ans = accumulated[i+2*k] - accumulated[i]
        else:
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            A =max(A, accumulated[i+2*k] - accumulated[i])
            ans = accumulated[i+2*k+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)



Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Accepted