Problem:
 Score : 400 points    Problem Statement 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.      Constraints  N is an integer satisfying 1 \leq N \leq 10^5 .  K is an integer satisfying 1 \leq K \leq 10^5 .  The length of the string S is N .  Each character of the string S is 0 or 1 .         Input Input is given from Standard Input in the following format:  N  K  S       Output Print the maximum possible number of consecutive people standing on hands after at most K directions.        Sample Input 1 5 1
00010      Sample Output 1 4  We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:   Give the direction with l = 1, r = 3 , which flips the first, second and third persons from the left.        Sample Input 2 14 2
11101010110011      Sample Output 2 8       Sample Input 3 1 1
1      Sample Output 3 1  No directions are necessary.

Solution (Python):
n,k=map(int,input().split())
s=list(map(int,list(input())))
t=[0]+[1 if s[0]==1 else -1]
ans=0
for i in range(1,n):
    if s[i]==s[i-1]:
        t[-1]=i+1 if s[i]==1 else -i-1
    else:
        t+=[i+1 if s[i]==1 else -i-1]
for i in range(len(t)-1):
    temp=0
    l=k*2+1 if t[i+1]>0 else k*2
    temp=abs(t[min(len(t)-1,i+l)])-abs(t[i])
    ans=max(ans,temp)
print(ans)

Solution (Python):
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(2)]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    if s[i]=='1':
      arr[0].append(count)
      count=1
    else:
      arr[1].append(count)
      count=1
if len(arr[1])<=k:
  print(n)
else:
  if s[0]=='0':
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
  arr[0].append(0)
  arr[1].append(0)
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])-1):
    sum+=arr[0][i]
    sum+=arr[1][i]
    count+=1
    if flag:      
      sum-=arr[0][i-k]
      sum-=arr[1][i-k]
    if count==k:
      tmp=sum
      tmp+=arr[0][i+1]
      if tmp>max:
        max=tmp
      count-=1
      flag=True
  print(max)

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

start = [0]
end = []

if s[0] == "0":
    end.append(0)

for i, (c0, c1) in enumerate(zip(s, s[1:])):
    if c0 == "1" and c1 == "0":
        end.append(i + 1)
    elif c0 == "0" and c1 == "1":
        start.append(i + 1)

if s[-1] == "1":
    end.append(n)
if n > 1 and s[-1] == "0":
    end.append(n)
    start.append(n)

k = min(k, len(start) - 1)

print(max(en - st for st, en in zip(start, end[k:])))

Solution (Python):
def main():
  from itertools import groupby

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

  num = []
  cot = []
  for k, n in groupby(S):
    num.append(k)
    cot.append(len(list(n)))

  if num[-1] == "0":
    cot += [0]
    num += [0]

  X = [0]*(len(cot)+1)
  for i in range(len(cot)):
    X[i+1] = X[i]+cot[i]

  ans = 0
  for i in range(len(X)):
    x = K*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, X[i+x]-X[i])
    except:
      break
  if ans == 0:
    ans = len(S)
  print(ans)

if __name__ == '__main__':
  main()

Solution (Python):
t,s=open(0)
n,k=map(int,t.split())
l=2*k+1
x=[0]*(2+int(s[0]))+[i+1for i in range(n-1)if s[i]!=s[i+1]]+[n]*l
print(max(x[i+l]-x[i]for i in range(0,len(x)-l,2)))

Solution (Python):

N, K = map(int, input().split())
S = input()
L=[0]


tmp="1"
for i in range(N):
    if tmp!=S[i]:
        L.append(1)
    else:
        L[-1]+=1
    tmp=S[i]
if tmp=="0":
    L.append(0)
M=len(L)
if M<=2*K+1:
    print(N)
    exit()
X=[0 for i in range(M+1)]
for i in range(M):
    X[i+1]=L[i]+X[i]
ans=0
for i in range(0,M-2*K,2):
    ans=max(ans,X[i+2*K+1]-X[i])
print(ans)

Solution (Python):
n,k = map(int,input().split())

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert(0,[1,0])
if ll[-1][0] == 0:
    ll.append([1,0])
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
co = 0
for i in range(2*k+1):
    co += ll[i][1]
lll.append(co)
for i in range(ppp-k):
    co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
    lll.append(co)
print(max(lll))

Solution (Python):
from itertools import groupby
n, k = map(int, input().split())
s = input()

t = []
if s[0] == "0":
    t.append(("1", 0))
for c, v in groupby(s):
    t.append((c, sum(1 for _ in v)))

if s[-1] == "0":
    t.append(("1", 0))

S = [t[0][1]]
rm = [0]
m = len(t)
for i in range(m//2):
    tmp = S[-1]
    S.append(tmp+t[2*i+1][1]+t[2*i+2][1])
    rm.append(tmp+t[2*i+1][1])

ans = []
for i in range(m//2+1):
    if i - k < 0:
        ans.append(S[i])
    else:
        ans.append(S[i]-rm[i-k])
print(max(ans))

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

index_lst = []
num_lst = []
before = '-1'
for K, i in enumerate(s):
    if i != before:
        before = i
        index_lst.append(K)
        num_lst.append(int(i))
N = len(index_lst)
index_lst += [n] * (2*k+10)

S = 0
for i in range(N):
    S = max(S, index_lst[i+2*k] - index_lst[i]) if num_lst[i] == 0 else max(S, index_lst[i+2*k+1] - index_lst[i])
print(S)

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

index_lst = []
num_lst = []
before = '-1'
for K, i in enumerate(s):
    if i != before:
        before = i
        index_lst.append(K)
        num_lst.append(int(i))
N = len(index_lst)
index_lst += [n] * (2*k+10)



S = 0
for i in range(N):
    if num_lst[i] == 0:
        S = max(S, index_lst[i+2*k] - index_lst[i])
    else:
        S = max(S, index_lst[i+2*k+1] - index_lst[i])
print(S)

Solution (Python):
n, k = map(int, input().rstrip().split())
s = input()
li1 = []
li2 = []
A = []
if n == 1:
    ans = 1
else:
    for i in range(n - 1):
        if s[i] == "0" and s[i + 1] == "1":
            li1.append(i)
        elif s[i] == "1" and s[i + 1] == "0":
            li2.append(i)
    if s[n - 1] == "0":
        li1.append(n - 1)
    if len(li1) <= k:
        ans = n
    else:
        if s[0] == "1":
            for j in range(len(li1) - k + 1):
                if j == 0:
                    A.append(li2[k] + 1)
                elif j == len(li1) - k:
                    A.append(n - li1[j - 1] - 1)
                else:
                    A.append(li2[k + j] - li1[j - 1])
        else:
            for j in range(len(li1) - k + 1):
                if j == 0:
                    A.append(li2[k - 1] + 1)
                elif j == len(li1) - k:
                    A.append(n - li1[j - 1] - 1)
                else:
                    A.append(li2[k + j - 1] - li1[j - 1])
        ans = max(A)
print(ans)

Solution (Python):
def solve():
  N, K = map(int, input().split())
  s = input()
  S = []
  look = s[0]
  cnt = 0

  if s[0] == '0': S.append(0)
  for c in s:
    if look == c: cnt += 1
    else:
      S.append(cnt)
      cnt = 1
      look = c
  S.append(cnt)
  if s[-1] == '0': S.append(0)

  num = min(len(S), 2 * K + 1)
  part = sum(S[0:num])
  ans = part

  for i in range(0, len(S) - num, 2):
    part += - sum(S[i:i+2]) + sum(S[i + num:i + num + 2])
    ans = max(ans, part)
  return ans

print(solve())

Solution (Python):
import bisect;n,k=map(int,input().split());s=input();f=[0]*n;f[0]=1-int(s[0])
for i in range(1,n):f[i]=(1-int(s[i]))*int(s[i-1])
for i in range(n-1):f[i+1]+=f[i]
a=0
for l in range(n):r=bisect.bisect_right(f,k+f[l]*int(s[l]));a=max(a,r-l)
print(a)

Solution (Python):



import numpy as np

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

s = input()

now = s[0]
tmp = 0

h = []
if s[0]=="0":
    h.append(0)

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

h = np.array(h)

ans = 0

for i in range(0,len(h),2):
    ans = max(ans,np.sum(h[i:i+k*2+1]))
    
print(ans)

Solution (Python):
n,k = map(int,input().split())

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert(0,[1,0])
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
co = 0
if ll[-1][0] == 1:
    for i in range(2*k+1):
        co += ll[i][1]
    lll.append(co)
    co -= ll[0][1] + ll[1][1]
    for i in range(ppp-k):
        if i == 0:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
            lll.append(co)
        else:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
            lll.append(co)
else:
    for i in range(2*k+1):
        co += ll[i][1]
    lll.append(co)
    co -= ll[0][1] + ll[1][1]
    for i in range(ppp-k):
        if ppp-k-1 == i and i == 0:
            co += ll[-1][1]
            lll.append(co)
        elif ppp-k-1 == i:
            co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
            lll.append(co)
        elif i == 0:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
            lll.append(co)
        else:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
            lll.append(co)
print(max(lll))

Solution (Python):
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

A.append(0)
A.append(0)

val = 0

if S[0] == "0":
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[2*K:2*K+2])
	val += new - old
	if val > currentMax:
		currentMax = val
	
	for i in range(1, len(A)-2*K, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Solution (Python):
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i if Y[0] < Z[0] else i-1])
    print(result)

Solution (Python):
import bisect
n,k=map(int,input().split())
s=list(map(int,input()))
f=[0]*n
f[0]=1-s[0]
for i in range(1,n):f[i]=(1-s[i])*s[i-1]
for i in range(n-1):f[i+1]+=f[i]
a=0
for l in range(n):
    r=bisect.bisect_right(f,k+f[l]*int(s[l]))
    a=max(a,r-l)
print(a)

Solution (Python):
N,K=map(int,input().split())
S=input()
l=[]
count=0
if S[0]=="0":
    now=0
    l+=[0]
    a=0
else:
    now=1
    a=1
for i in range(len(S)):
    if int(S[i])==now:
        count+=1
    else:
        l+=[count]
        count=1
        now=int(S[i])
l+=[count]
l+=[0]

ans=sum(l[:2*K+1])
s=sum(l[:2*K+1])
if len(l)-2*K>0:
    for i in range(2*K+2,len(l),2):
        s+=l[i]+l[i-1]-l[i-2*K-1]-l[i-2*K-2]
        ans=max(ans,s)
else:
    ans=sum(l)



if len(l)==1 and a==1:
    print(l[0])
else:
    print(ans)

Solution (Python):
N,K=map(int,input().split());s=input();L=2*K+1;X=[0]*(2-int(s[0]))+[i+1 for i in range(N-1) if s[i]!=s[i+1]]+[N]*L;print(max([X[i+L]-X[i]for i in range(0,len(X)-L,2)]))

Solution (Python):



import numpy as np

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

s = input()

now = s[0]
tmp = 0

h = []
if s[0]=="0":
    h.append(0)

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

h = np.array(h)

ans = 0

for i in range(0,max(len(h),len(h)-k),2):
    ans = max(ans,np.sum(h[i:i+k*2+1]))
    
print(ans)

Solution (Python):
n,k = map(int,input().split())

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0

if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)

    else:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)
else:
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
print(max(lll))

Solution (Python):
import bisect
n,k=map(int,input().split())
s=list(map(int,input()))
f=[0 for i in range(n)]
f[0]=1-s[0]
for i in range(1,n):f[i]=(1-s[i])*(s[i-1]^s[i])
for i in range(n-1):f[i+1]+=f[i]
ans=0
for l in range(n):
    r=bisect.bisect_right(f,k+f[l]*s[l])
    ans=max(ans,r-l)
print(ans)

Solution (Python):
n, k = map(int, input().split())
s = list(map(int, input()))
x = [0]
if s[0] == 0:
    x.append(0)
for i in range(n-1):
    if s[i] != s[i+1]:
        x.append(i+1)
x.append(n)
if s[n-1] == 0:
    x.append(n)
r = len(x) // 2 - 1
if k >= r:
    print(n)
else:
    print(max(x[2*(i+k)+1]-x[2*i] for i in range(r-k+1)))

Solution (Python):
def binarySearch(alist, item):
    first = 0
    last = len(alist) - 1

    while first <= last:
        midpoint = (first + last)//2
        if item < alist[midpoint] and alist[midpoint - 1] < item or midpoint == 0 and item < alist[midpoint]:
            return midpoint
        else:
            if item < alist[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return -1


def load_input():
    n, k = list(map(int, input().split()))
    s = list(input())
    return n, k, s


def main():
    n, k, s = load_input()

    starts = [0]
    ends = []
    for i in range(n):
        if 0 < i and s[i - 1] == '1' and s[i] == '0':
            ends.append(i)

        if 0 < i and s[i - 1] == '0' and s[i] == '1':
            starts.append(i)
    ends.append(n)

    ml = 0
    for start in starts:
        order = k
        if start == 0 and s[0] == '0':
            order -= 1
        i = binarySearch(ends, start)
        if i + order < len(ends):
            end = ends[i + order]
        else:
            end = ends[-1]
        if ml < end - start:
            ml = end - start
    print(ml)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

l = [[], []]
ans = []
k = [int(s) for s in S]
if k[0] == 0:
    l[1].append(0)
v0 = 1 if k[0] == 0 else 0
v1 = 1 if k[0] == 1 else 0
for i in range(1, N):
    if (k[i]==0) & (k[i-1]==0):
        v0 += 1
    elif (k[i]==1) & (k[i-1]==0):
        l[0].append(v0)
        v0 = 0
        v1 += 1
    elif (k[i]==0) & (k[i-1]==1):
        l[1].append(v1)
        v0 += 1
        v1 = 0 
    else:
        v1 += 1
        
for i, v in enumerate([v0, v1]):
    if v > 0:
        l[i].append(v)
        
if k[0] == 0:
    l[1].append(0)

if len(l[0]) <= K:
    print(N)

else:
    l[0].extend(np.repeat(0,K+1))
    l[1].extend(np.repeat(0,K+1))
    a = sum(l[0][:K]) + sum(l[1][:K+1])
    ans.append(a)
    for i in range(1, len(l[0])-K-1):
        a -= l[0][i-1] + l[1][i-1]
        a += l[0][i+K-1] + l[1][i+K]
        ans.append(a)
    print(max(ans))

Solution (Python):
N, K = list(map(int, input().split()))
S = input()

S = S + 'E'   
ans = -1    


if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0


while (R < N):
    
    if (stand <= K):
        
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        
        ans = max(ans, R - L + 1)
        R += 1
    
    
    elif (L == R):
        L += 1
        R += 1
    
    else:
        
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1
        

print(ans)

Solution (Python):
n,k=map(int, input().split())
s=input()
st=[0]
for i in range(1,n):
    if(s[i]!=s[i-1]):
        st.append(i)
st.append(n)
a=len(st)-1
if(s[0]=="0"):
    ans=st[min(2*k,a)]
    for i in range(1,a+1,2):
        ans=max(ans,st[min(2*k+1+i,a)]-st[i])
elif(s[0]=="1"):
    ans=0
    for i in range(0,a+1,2):
        ans=max(ans,st[min(2*k+1+i,a)]-st[i])
print(ans)

Solution (Python):
import math                                                                                            

n,k = raw_input().split()
s = list(raw_input())


cnt = 0

prev = ''
tmp = ''
ar = []
for i in s:
    if i == '0' and prev !=  1:
        tmp += '0'
        prev = 0
    elif i == '0' and prev == 1:
        prev = 0
        ar.append(tmp)
        tmp = '0'
    elif i == '1' and prev != 0:
        tmp += '1'
        prev = 1
    elif i == '1' and prev == 0:
        prev = 1
        ar.append(tmp)
        tmp = '1'

ar.append(tmp)
k = int(k)


j = 0
cnt = 0
total = 0
m = 0

for i in ar:
    total += len(i)
    if '0' in i:
        cnt += 1
        if cnt == k + 1:                           
            total -= len(ar[j-2*k]) 
            if j >= 2*k + 1:                       
                total -= len(ar[j-2*k-1])
            cnt = k
    m = max(m, total)
    j += 1

print m

Solution (Python):
n,k = [int(i) for i in input().split()]
s = input()

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        start_index = 1
        score1 = groups[start_index-1]
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k +1 + 2 * i]
            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        start_index = 0
        score1 = 0
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]

        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            if i >=1:
                score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]

            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

Solution (Python):
N, K = map(int, input().split())
S = input();

mans = [];
seq = 0;
now = S[0];
if(now == "0"):
    mans.append(0);

for i in S:
    if(i == now):
        seq += 1;
    else:
        mans.append(seq);
        seq = 1;
        if(now=="1"):
            now="0"
        else:
            now="1"

if(seq!=0):
    mans.append(seq);
    if(now=="1"):
        now="0"
    else:
        now="1"
if(now=="0"):
    mans.append(0);

mans.append(0);

shakutori = [0]
shakunum = 0;

for i in mans:
    shakunum += i;
    shakutori.append(shakunum);

if(len(shakutori) <= 2*K+1):
    print(shakutori[-1]);
else:
    answer = 0;
    for i in range(1,len(shakutori),2):
        if(len(shakutori) <= i+2*K):
            break;
        else:
            if(shakutori[i+2*K]-shakutori[i-1] > answer):
                answer = shakutori[i+2*K]-shakutori[i-1];
    print(answer);

Solution (Python):
n, k = [int(i) for i in input().split()]
S = input()

cum = [0]
last = S[0]
cnt = 0
for s in S:
    if s == last:
        cnt += 1
    else:
        cum.append(cum[-1] + cnt)
        cnt = 1
        last = s
cum.append(cum[-1] + cnt)

ans = 0
start = 0 if S[0] == '1' else 1
for i in range(start, len(cum), 2):
    ans = max(ans, cum[min(i + 2 * k + 1, len(cum) - 1)] - cum[i])

if S[0] == '0':
    ans = max(ans, cum[min(2 * k, len(cum) - 1)])

print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
S = input()

S = S + 'E'   
ans = -1    


if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0


while (R < N):
    
    if (stand <= K):
        
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        ans = max(ans, R - L + 1)
        R += 1
    
    
    elif (L == R):
        L += 1
        R += 1
    
    else:
        
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1

print(ans)

Solution (Python):
def solve():
  N, K = map(int, input().split())
  s = input()
  S = []
  b = s[0]
  cnt = 0

  if s[0] == '0': S.append(0)
  for c in s:
    if b == c: cnt = cnt + 1
    else:
      b = c
      S.append(cnt)
      cnt = 1
  S.append(cnt)
  if s[-1] == '0': S.append(0)

  num = min(len(S), 2 * K + 1)
  part = sum(S[0:num])
  ans = part

  for i in range(0, len(S) - num, 2):
    part = part - sum(S[i:i+2]) + sum(S[i + num:i + num + 2])
    ans = max(ans, part)

  return ans

print(solve())

Solution (Python):
n, k = list(map(int, input().split()))
s = str(input())
prev = '1'
r = []
for i in range(n):
  if s[i] != prev:
    if s[i] == '0':
      f = i
    else:
      r.append((f, i))
  prev = s[i]
if s[-1] == '0':
  r.append((f, n))
if k >= len(r):
  print(n)
else:
  a = 0
  for i in range(len(r) - k + 1):
    if i == 0:
      begin = 0
    else:
      begin = r[i-1][1]
    if i == len(r) - k:
      end = n
    else:
      end = r[i+k][0]
    a = max(a, end - begin)
  print(a)

Solution (Python):
import numpy
N,K=(int(x) for x in input().split())
S=input()

def Solve(S,K):

    if S.count("0")<=K:
        return len(S)
    if S[0]=="1":
        S="0"+S
    

    LA=[]
    LB=[]
    k=-1
    current="0"
    appendLA=LA.append
    appendLB=LB.append
    for i,s in enumerate(S):
        k+=1
        if s!=current:
            if s=="0":
                appendLA(k)
                current="0"
                k=0
            else:
                appendLB(k)
                current="1"
                k=0
    
    if current=="1":
        appendLA(k+1)
    else:
        appendLB(k+1)
        appendLA(0)
    mx=0
    if len(LA)<=K:
        return len(S)
    if K==0:
        return max(LA)

    sum_LA = list(numpy.cumsum(LA))
    sum_LB=list(numpy.cumsum(LB))
    

    for i in range(len(LA)-K+1):
        if i==0:
            result=sum_LA[K-1]+sum_LB[K-1]
        elif i==1:
            result=sum_LA[i+K-1]+sum_LB[i+K-1]-sum_LB[i-1]
        else:
            result=sum_LA[i+K-1]-sum_LA[i-2]+sum_LB[i+K-1]-sum_LB[i-1]
        mx=max(mx,result)

    return mx

print(Solve(S,K))

Solution (Python):
N, K =  map(int, (input().split(' ')))
S = str(input())

zero_positions = [0] + [i for i, c in enumerate(S) if c==0]
count0 = 0
start = 0
end = 0
numbers = 0
index = 0
load_0 = False
l_0 = False
scores = []
while True:
    if (not load_0) and S[index] == '0':
        count0 += 1
        load_0 = True
    elif load_0 == True and S[index] == '1':
        load_0 = False
    if count0 == K+1:
        scores.append((index-1) - start + 1)
        if (not l_0) and S[start] == '0':
            l_0 = True
        while count0 > K:
            start += 1
            if (not l_0) and S[start] == '0':
                l_0 = True
            elif l_0 == True and S[start] == '1':
                count0 -= 1
                l_0 = False
    if index == N - 1 :
        scores.append(index-start + 1)
        break
    index += 1
        
print(max(scores))

Solution (Python):

import sys
import math
import numpy as np
import itertools


n,k = (int(i) for i in input().split())  


s = [int(i) for i in list(input())] 

j=0
t = [ [s[0],1]]

for i in range(1,n):
    if s[i] == t[j][0]:
        t[j][1] += 1
    else:
        j += 1
        t.append([s[i],1])


if t[0][0] == 0:
    t.insert(0,[1,0])
if t[-1][0] == 0:
    t.append([1,0])


z = len(t) // 2

if z <= k:
    print(n)
    exit()
m = 0
for i in range(k*2+1):
    m += t[i][1]
ma = m

t.append([1,0])

for i in range(z-k):
    m += (t[k*2+i*2+1][1]+t[k*2+i*2+2][1])
    m -= (t[i*2][1]+t[i*2+1][1])
    ma = max(ma,m)


print(ma)

Solution (Python):
def solve():
  N, K = map(int, input().split())
  _S = input()
  S = []
  b = _S[0]
  s = 0
  if b == '0': S.append(0)
  for _s in _S:
    if b == _s:
      s = s + 1
    else:
      b = _s
      S.append(s)
      s = 1
  S.append(s)
  if b == '0': S.append(0)
 
  num = min(len(S), 2 * K + 1)
  
  n = sum(S[0:num])
  m = n
  
  for i in range(0, len(S) - num, 2):
    n = n - sum(S[i:i+2])
    n = n + sum(S[i + num:i + num + 2])
    m = max(m, n)
 
  return m
 
print(solve())

Solution (Python):
n, k = map(int, input().split())
S = list(map(int, input()))
T = []    
if S[0] == 1:
    T.append(1)
else:
    T.append(0)
    T.append(-1)
for i in range(1, n):
    if S[i] == S[i-1] and S[i] == 1:
        T[-1] += 1
    elif S[i] == S[i-1]:
        T[-1] -= 1
    elif S[i] == 1:
        T.append(1)
    else:
        T.append(-1)
if T[-1] < 0:
    T.append(0)
m = len(T)

ans = []
ans.append(T[0])
for i in range(0, k):
    if 2*i+2 < m:
        ans[0] = ans[0] - T[2*i+1] + T[2*i+2]
    else:
        break

i = 0
j = k+1
ma = ans[0]
while True:
    if 2*j >= m:
        break
    re = ans[-1] - T[2*i] + T[2*i+1] - T[2*j-1] + T[2*j]
    ma = max(ma, re)
    i += 1
    j += 1
    ans.append(re)
    
    
print(ma)

Solution (Python):
N, K = list(map(int, input().split()))
S = list(input())

L = [1]

for i in range(1, N):
	if S[i] == S[i - 1]:
		L[-1] += 1
	else:
		L.append(1)

n = len(L)
if S[0] == "1":
	if n // 2 <= K:
		print(N)
	else:
		if S[-1] == "1":
			x = L[0]
			for i in range(K):
				x += L[2 * i + 1] + L[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K):
				val += L[2 * (i + K) + 1] + L[2 * (i + K) + 2] - L[2 * i] - L[2 * i + 1]
				if val > x:
					x = val
			print(x)
		else:
			x = L[0]
			for i in range(K):
				x += L[2 * i + 1] + L[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K - 1):
				val += L[2 * (i + K) + 1] + L[2 * (i + K) + 2] - L[2 * i] - L[2 * i + 1]
				if val > x:
					x = val
			val += L[-1] - L[-2 * K - 1] - L[-2 * K - 2]
			if val > x:
				x = val
			print(x)			

else:
	if (n + 1) // 2 <= K:
		print(N)
	elif (n - 1) // 2 == K and S[0] == "0" and S[-1] == "0":
		print(max(N - L[0], N - L[-1]))
	else:
		if S[-1] == "1":
			LL = [L[-1 - i] for i in range(n)]
			x = LL[0]
			for i in range(K):
				x += LL[2 * i + 1] + LL[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > x:
					x = val
			val += L[0] - LL[-2 * K + 1] - LL[-2 * K]
			if val > x:
				x = val
			print(x)
		else:
			LL = L[1:]
			x = LL[0]
			for i in range(K):
				x += LL[2 * i + 1] + LL[2 * i + 2]
			val = x
			for i in range(int((n - 1) // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > x:
					x = val
			val += L[-1] - LL[-2 * K + 1] - LL[-2 * K]
			if val > x:
				x = val

			LL = [L[-2 - i] for i in range(n - 1)]
			y = LL[0]
			for i in range(K):
				y += LL[2 * i + 1] + LL[2 * i + 2]
			val = y
			for i in range(int((n - 1) // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > y:
					y = val
			val += L[0] - LL[-2 * K + 1] - LL[-2 * K]
			if val > y:
				y = val
			print(max(x, y))

Solution (Python):
N, K = map(int, input().split())
people = list(input())
lst = []
count = 0
flag = people[0]
if flag == '1':
	lst.append(0)
for i in range(len(people)):
	if i == len(people) - 1:
		lst.append(count + 1)
	elif people[i] == flag and people[i+1] != flag:
		lst.append(count + 1)
		count = 0
		flag = people[i + 1]
	else:
		count += 1

maximum = 0
l = len(lst)
temp = sum(lst[0:2*K])
maximum = max(maximum, temp)
for i in range(2, l, 2):
	end = i+2*K
	temp = temp - lst[i-2] + sum(lst[end-2:end])
	if i != 2:
		temp -= lst[i-3]
	maximum = max(maximum, temp)

print(maximum)

Solution (Python):
N, K = map(int, input().split())
S = input()
a = []
if S[0] == '0':
    a.append(0)
cnt = 1
for i in range(1, N):
    if S[i] != S[i - 1]:
        a.append(cnt)
        cnt = 1
    else:
        cnt += 1

a.append(cnt)
a.append(0)
a.append(0)

ans = s = sum(a[:2*K + 1])
for i in range(2*K + 2, len(a), 2):
    s += a[i] + a[i - 1] - a[i - 2*K - 1] - a[i - 2*K - 2]
    ans = max(ans, s)
print(ans)