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 = input()
memo =[]
now = '1'
cnt = 0
for s in S:
    if s==now:
        cnt+=1
    else:
        memo.append(cnt)
        cnt=1
        now = '1' if now=='0' else '0'
else:
    memo.append(cnt)
if S[-1] == '0':
    memo.append(0)
ans = 0
add = K*2+1

ruiwa = [0]*(len(memo)+1)
for i in range(len(memo)):
    ruiwa[i+1]=ruiwa[i]+memo[i]

for i in range(N)[::2]:
    if i < N:
        tmp = ruiwa[min(i+add, len(ruiwa)-1)] - ruiwa[min(i, len(ruiwa)-1)]
        ans = max(ans, tmp)
print(ans)

Solution (Python):
import sys
inp=[i.strip() for i in sys.stdin.readlines()]
if len(inp)==1:
    if len(inp[0].split())==2:
        A,B=map(int,inp[0].split())
        print(max(2*A-1,2*B-1,A+B))
    else:
        S=inp[0]
        N=len(S)
        X=""
        Y=""
        for i in range(N):
            if i%2==0:
                X=X+"0"
                Y=Y+"1"
            else:
                X=X+"1"
                Y=Y+"0"
        A=0
        B=0
        for i in range(N):
            if X[i]!=S[i]:
                A+=1
            if Y[i]!=S[i]:
                B+=1
        print(min(A,B))
else:
    if len(inp[0].split())==1:
        N=int(inp[0])
        H=[int(i) for i in inp[1].split()]
        ans=1
        for i in range(1,N):
            flag=1
            for j in range(i):
                if H[j]>H[i]:
                    flag=0
                    break
            if flag==1:
                ans+=1
        print(ans)
    else:
        N,K=map(int,inp[0].split())
        S=inp[1]
        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):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def POW(x, y):
    if y == 0:
        return 1
    elif y == 1:
        return x
    elif y % 2 == 0:
        return POW(x, y // 2) ** 2 % MOD
    else:
        return POW(x, y // 2) ** 2 * x % MOD
def mod_factorial(x, y): return x * POW(y, MOD - 2) % MOD
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from collections import Counter
from itertools import accumulate

def main():
    N, K = LI()
    S = SI()
    cnts = []
    pre = '1'
    cnt = 0
    
    for s in S:
        if s != pre:
            cnts.append(cnt)
            cnt = 0
            pre = s
        cnt += 1
    cnts.append(cnt)
    acc = [0] + list(accumulate(cnts))
    ans = 0
    for i in range(0, len(acc) - 1, 2):
        tmp = acc[min(len(acc) - 1, i + 2 * K + 1)] - acc[i]
        ans = max(tmp, ans)
        if len(acc) < i + 2 * K + 1:
            break

    return ans

print(main())

Solution (Python):
import sys

n, k = [int(x) for x in input().split()]
s = input()
l = [0]
a = '1'
for i in range(n):
  if s[i] != a:
    l.append(i)
    a = s[i]
l.append(n)
if s[-1] == '0':
  l.append(n)
if len(l) < 2 * k + 2:
  print(n)
  sys.exit()
a, ans = 0, 0
while a + 2 * k + 1 < len(l):
  b = l[a+2*k+1] - l[a]
  if ans < b:
    ans = b
  a += 2
print(ans)

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

if len(Nums) % 2 == 0:
    Nums.append(0)

add = 2*K+1
ans = 0
left, right = 0, 0
tmp = 0  
for i in range(len(Nums))[::2]:
    next_left = i
    next_right = min(i + add, len(Nums))
    while next_left > left:
        tmp -= Nums[left]
        left += 1
    while next_right > right:
        tmp += Nums[right]
        right += 1

    ans = max(tmp, ans)

print(ans)

Solution (Python):
n, k = map(int, input().split())
S = input()
tmp = S[0]
count = 0
num_count = []
f = int(S[0])
for s in S:
    if tmp == s:
        count += 1
    else:
        num_count.append(count)
        count = 1
    tmp = s
num_count.append(count)
if S[0] == '0':
    num_count.insert(0, 0)
if S[-1] == '0':
    num_count.insert(-1, 0)
sum = [0]
for i in num_count:
    sum.append(sum[-1] + i)
a = 0
for i, num in enumerate(sum[::2]):
    left = i * 2
    right = min(i * 2 + 2 * k + 1, len(num_count))
    t = sum[right] - sum[left]
    a = max(a, t)
print(a)

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if sss[-1] == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])

for i in range(len(x))[::2]:
    left = i
    right = min(i + Add, len(x))
    tmp = y[right] - y[left]    
    
    ans = max(tmp, ans)

print(ans)

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if len(x) % 2 == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])

for i in range(len(x))[::2]:
    left = i
    right = min(i + Add, len(x))
    tmp = y[right] - y[left]    
    
    ans = max(tmp, ans)

print(ans)

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if len(x) % 2 == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

left = 0
right = 0
tmp = 0

for i in range(len(x))[::2]:
    nextleft = i
    nextright = min(i + Add, len(x))
    while nextleft > left:
        tmp -= x[left]
        left += 1
    while nextright > right:
        tmp += x[right]
        right += 1
    
    ans = max(tmp, ans)

print(ans)

Solution (Python):
from collections import deque

N, K = map(int, input().split())
S = [int(x) for x in input()]

count = 0
flag = True

la = deque([])
for c in S:
    if int(flag) == c:
        count += 1
    else:
        la.append(count)
        count = 1
        flag = not flag

la.append(count)

if K >= (len(la) / 2):
    print(N)
    exit()

ltmp = deque([])
sum_la = 0
for _ in range((K-1)*2 + 1):
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)

last_a = [sum_la]
while len(la) > 0 :
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)
    if len(la) > 0:
        tmp = la.popleft()
        sum_la += tmp
        ltmp.append(tmp)

    last_a.append(sum_la)

    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    last_a.append(sum_la)

print(max(last_a))

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

cnt = 0
state = sss[0]
x = []
if sss[0] == "0":
    x.append(0)
for s in sss:
    if s == state:
        cnt += 1
    else:
        state = s
        x.append(cnt)
        cnt = 1
x.append(cnt)
if sss[-1] == "0":
    x.append(0)







y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])





z = []
for i in range(k*2+1,len(y))[::2]:
    z.append(y[i] - y[i - (k*2+1)])



if len(x) <= k*2-1:
    print(sum(x))
else:
    print(max(z))

Solution (Python):
from itertools import accumulate

N, K = map(int, input().split())
S = [int(x) for x in input()]

nums = []
now, cnt = 1, 0
for s in S:
    if s == now:
        cnt += 1
    else:
        nums.append(cnt)
        now, cnt = 1 - now, 1
if cnt != 0:
    nums.append(cnt)
if len(nums) % 2 == 0:
    nums.append(0)

sums = [0] + list(accumulate(nums))

ans = 0
add = 2 * K + 1
for i in range(0, len(nums), 2):
    left = i
    right = min(i + add, len(nums))
    ans = max(ans, sums[right] - sums[left])
print(ans)

Solution (Python):




[N, K] = list(map(int,input().split()))
S = str(input())
tmp = 1
list_S = []
for i in range(N):
    if i == N-1:
        list_S.append(tmp)
        break
    if S[i] != S[i+1]:
        list_S.append(tmp)
        tmp = 1
    else:
        tmp += 1
M = len(list_S)+1
list_S = list_S + [0]*(2*K+5)
if S[0] == "0":
    list_S = [0] + list_S
    
tmp = sum(list_S[0:2*K + 1])
ans = sum(list_S[0:2*K + 1])
for i in range(0,M+2,2):
    tmp = tmp - list_S[i] -list_S[i+1]
    tmp = tmp + list_S[i+2*K+1] + list_S[i+2*K+2]
    if tmp > ans:
        ans = tmp
print(ans)

Solution (Python):
def solve(n, k, s):
    v = []
    if s[0] == "0":
        v.append(0)
    i = 0
    while i < n:
        num = 1
        while (i+1 < n) and (s[i+1] == s[i]):
            num += 1
            i += 1
        v.append(num)
        i += 1
    if s[n-1] == "0":
        v.append(0)
    m = len(v)
    w = [0] * m
    w[0] = v[0]
    for i in range(m-1):
        w[i+1] = w[i] + v[i+1]
    best = w[0]
    for left in range(0, m, 2):
        right = min(left + 2*k + 1, m)
        if left == 0:
            cur = w[right-1]
        else:
            cur = w[right-1] - w[left-1]
        best = max(best, cur)
    return best

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

Solution (Python):
n,k = map(int,input().split())
s = input()
times = [1]
list_ans = []
for i in range(n-1):
        if s[i] != s[i+1]:
                times.append(i+2)
len_times = len(times)
for i in range(2*k+1):
        times.append(n+1)
for i in range(len_times):
        if s[times[i]-1] == "0":
                list_ans.append(times[i+2*k] - times[i])
        else:
                list_ans.append(times[i+2*k+1] - times[i])
print(max(list_ans))

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

cnts = []

tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: 
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)


n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
if n_zero <= k:
     ans = sum(cnts)

else:
    ans = 0
    cumsum = [0]
    for i in range(len(cnts)):
        cumsum.append(cumsum[-1] + cnts[i])

    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k , len(cnts))
        tmp = cumsum[end] - cumsum[start]
        ans = max(tmp, ans)

print(ans)

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

state = 1
cnt = 0
l = []
for e in s:
    if e != state:
        l.append(cnt)
        cnt = 1
        state = e
    else:
        cnt += 1

if cnt != 0:
    l.append(cnt)


if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]

for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    tmp = ruiseki[right] - ruiseki[left]
    answer = max(tmp, answer)

print(answer)

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

state = 1
cnt = 0
l = []
for e in s:
    if e != state:
        l.append(cnt)
        cnt = 1
        state = e
    else:
        cnt += 1

if cnt != 0:
    l.append(cnt)

if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]

for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    tmp = ruiseki[right] - ruiseki[left]
    answer = max(tmp, answer)
 
print(answer)

Solution (Python):
import copy
def main():
    n, k = map(int, input().rstrip().split(' '))
    s = [c == '1' for c in input().rstrip()]

    windows = []
    current_window = None
    for i, c in enumerate(s):
        if not c:
            if current_window  == None:
                current_window = Window(False)
            elif current_window.one:
                windows.append(current_window)
                current_window = Window(False)
            current_window.count += 1
        else:
            if current_window  == None:
                current_window = Window(True)
            elif not current_window.one:
                windows.append(current_window)
                current_window = Window(True)
            current_window.count += 1
    windows.append(current_window)
    
    counts = []
    count = 0
    zero_window_count = 0
    for i, w in enumerate(windows):
        if not w.one:
            zero_window_count += 1
            if zero_window_count > k:
                counts.append(count)
                leftmost = i - (2 * k) - 1
                if leftmost >= 0:
                    count -= windows[leftmost].count 
                count -= windows[leftmost + 1].count 

        count += w.count
    counts.append(count)
    print(max(counts))

class Window:
    def __init__(self, one_or_zero):
        self.one = one_or_zero
        self.count = 0
    
    def __repr__(self):
        return ("1" if self.one else "0") + " " + str(self.count)


if __name__ == '__main__':
    main()

Solution (Python):
N,K = list(map(int,input().split()))
s = input()
data = [int(a) for a in s]



nums = []
if data[0]==0:
	nums.append(0)
i,j = 0,0
for i in range(len(data)):
	i,count = j,1
	for j in range(i+1,len(data)):
		if data[i]==data[j]:
			count+=1
		if data[i]!=data[j] or j==len(data)-1:
			nums.append(count)
			break


sums = [0]
for i in range(len(nums)):
	sums.append(sums[i] + nums[i])


ans = 1
for i in  range(0,len(sums),2):
	j = i + K*2+1
	if j>=len(sums):
		j = len(sums)-1
	ans = max(ans, sums[j]-sums[i])

print(ans)

Solution (Python):
N,K = map(int,input().split())
S = input()
if S[0] == '0':
  divide = []
else:
  divide = [0]
tmp = 1
for i in range(1,N):
  if S[i] == S[i-1]:
    tmp += 1
  else:
    divide += [tmp]
    tmp = 1
divide += [tmp]
le = len(divide)
zero = [0]*2*le + divide + [0]*2*le
su = [0]
for i in range(0,5*le):
  su += [su[-1]+zero[i]]
del su[0]
lle = len(su)
mxm = 0
for k in range(K,lle):
  if 2*k+1 >= lle:
    break
  cd = su[2*k+1] - su[2*k-2*K]
  if cd > mxm:
    mxm = cd
xmx = max(divide)
print(max(mxm,xmx))

Solution (Python):
from itertools import accumulate

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

s_num = []

prev = s[0]
cnt = 0
for c in s:
    if prev == c:
        cnt += 1
    else:
        s_num.append(cnt)
        cnt = 1
    prev = c
s_num.append(cnt)

if prev == '0':
    s_num.append(0)

if s[0] == '0':
    s_num = [0] + s_num
if s[-1] == '0':
    s_num = s_num + [0]

w = 2 * k + 1

if len(s_num) <= w:
    print(n)
else:

    acc = [0] + list(accumulate(s_num))
    res = [acc[i+w] - acc[i] for i in range(0, len(acc) - w, 2)]

    
    
    
    print(max(res))

Solution (Python):
from itertools import accumulate
 
n, k = list(map(int, input().split()))
s = input()
 
l = []

prev = '1'
cnt = 0
for c in s :
  if prev == c:
    cnt +=1 
  else :
    l.append(cnt)
    cnt = 1
  prev = c
l.append(cnt)

if prev == '0':
  l.append(0)

w = 2 * k + 1

if len(l)<= w :
  print(n)
else :
  acc = [0] + list(accumulate(l))
  ans = 0
  for i in range(0, len(l) - w + 1, 2):
    
    
    ans = max(ans, acc[i + w] - acc[i])
  print(ans)

Solution (Python):
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]


sum01 = []
pre = 1
cnt = 0


for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)


Add = 2 * k + 1
ruiseki = [0]
for i, e in enumerate(sum01):
    ruiseki.append(ruiseki[i] + sum01[i])


answer = 0
for i in range(0, len(sum01), 2):
    left = i
    right = min(i + Add, len(sum01))
    temp = ruiseki[right] - ruiseki[left]
    answer = max(temp, answer)

print(answer)

Solution (Python):
def Main():
    N, K = map(int, input().split())
    S = input()
    arr = []
    arr.append(0)
    if S[0] == '0':
        arr.append(0)
    cnt = 1
    for i in range(1, N):
        if S[i-1] == S[i]:
            cnt += 1
        else:
            arr.append(cnt)
            cnt = 1
    arr.append(cnt)
    n = len(arr)
    for i in range(1, n):
        arr[i] += arr[i-1]
    
    ans = 0
    sum = 0
    l = 0
    for r in range(1, n):
        while((r-l)//2 > K):
            l += 2
        
        ans = max(ans, arr[r]-arr[l]) 
       
    print(ans)

if __name__ == "__main__":
    Main()

Solution (Python):
N, K = list(map(int, input().split()))
S = input()
 
S += "X"
i = 0
j = 0
max_len = 0
change = 0 if S[0] != "0" else 1
while j < len(S)-1:
  if change <= K:
    
    max_len = max([j-i+1, max_len])
    if S[j:j+2] == "10":
      change += 1
    j += 1
  else:
    if S[i:i+2] == "01":
      change -= 1
    i += 1

print(max_len)

Solution (Python):
N,K=map(int,input().split())
S=input()
po=[0]
X=[]
i=0
for i in range(N-1):
	if not S[i] == S[i+1]:
		po.append(i+1)
po.append(len(S))
for i in range(len(po)):
	if 2*K+1>=len(po):
		X.append(po[-1])
		break
	elif 2*K+i+1>=len(po):
		X.append(po[2*K+i]-po[i])
		break
	elif S[po[i]]=='0':
		X.append(po[2*K+i]-po[i])
	else:
		X.append(po[2*K+1+i]-po[i])
print(max(X))

Solution (Python):
def calc_seq(xs):
    seq = []
    count = 1
    for i in range(0, len(xs)-1):
        if xs[i] == xs[i+1]:
            count += 1
        else:
            seq.append(count)
            count = 1
    seq.append(count)
    return seq

def form_seq(xs):
    seq = calc_seq(xs)
    if xs[0] == 0:
        seq.insert(0, 0)
    if len(seq) % 2 == 0:
        seq.append(0)
    return seq

def main(n, k, xs):
    seq = form_seq(xs)
    m = len(seq)
    i = 1
    if i + 2*k > m:
        print(n)
        return
    j = i - 1
    val = sum(seq[j:j+2*k+1])
    biggest = val
    i += 2
    while i + 2*k <= m:
        j = i - 1
        val += seq[j+2*k-1] + seq[j+2*k] - seq[j-2] - seq[j-1]
        if biggest < val:
            biggest = val
        i += 2
    print(biggest)

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

main(n, k, xs)

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

si = [1]
si.extend(2+i for i, (s1, s2) in enumerate(zip(S, S[1:])) if s1 != s2)
si.append(N + 1)


def f(i, si):
    return si[min(i + 2*K + 1, len(si)) - 1] - si[i-1]

j0 = min(1 + 2*K + (1 if S[0] == "1" else 0), len(si)) - 1
x0 = si[j0] - 1

x = [x0]
x.extend(f(i+1, si) for i in range(2 if S[0] == "1" else 1, len(si), 2))
print(max(x))

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

now_char = '1'
cnt = 0
cnt_list = []




for i, s in enumerate(S):
    
    
    if s != now_char:
        cnt_list.append(cnt)
        cnt = 1
        now_char = s
    
    else:
        cnt += 1
else:
    
    cnt_list.append(cnt)
    
    if S[-1] == '0':
        cnt_list.append(0)






if 2 * K + 1 >= len(cnt_list):
    ans = sum(cnt_list)  
else:
    l = 0
    r = 2 * K
    tmp_sum = sum(cnt_list[l:r+1])
    max_sum = 0
    while r < len(cnt_list):
        if tmp_sum > max_sum:
            max_sum = tmp_sum
        tmp_sum = tmp_sum + sum(cnt_list[r+1:r+3]) - sum(cnt_list[l:l+2])
        l += 2
        r += 2
    ans = max_sum

print(ans)

Solution (Python):
from itertools import accumulate
N, K = map(int, input().split())
S = input()
nums = []
if S[0] == '0':
    nums.append(0)
i = 0
while i < N:
    j = i
    while j < N and S[i] == S[j]:
        j += 1
    nums.append(j - i)
    i = j
if S[-1] == '0':
    nums.append(0)

sums = [0] * (len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
ans = 0
left = 0
while left < len(nums):
    right = left + 2 * K + 1
    if right > len(sums):
        right = len(sums) - 1
    ans = max(ans, sums[right]-sums[left])
    left += 2

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):
n, m = map(int, input().split())
s = input()
a = []
for i in range(n):
    if i == 0 or s[i] != s[i - 1]:
        a.append(1)
    else:
        a[len(a) - 1] += 1
for i in range(1, len(a)):
    a[i] += a[i - 1]
cur = int(s[0])
ans = 0
for i in range(len(a)):
    tmp = a[i]
    if cur:
        if i > 2 * m:
            tmp -= a[i - 2 * m - 1]
    else:
        if i > 2 * m - 1:
            tmp -= a[i - 2 * m]
    ans = max(ans, tmp)
    cur = 1 - cur
print(ans)

Solution (Python):
import itertools

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

if s[0] == '0':
    seq.append(0)

i = 1
seq_cnt = 1
while i != n:
    if s[i] != s[i-1]:
        seq.append(seq_cnt)
        seq_cnt = 1
    else:
        seq_cnt += 1
    i += 1

seq.append(seq_cnt)
if s[-1] == '0':
    seq.append(0)

b = [0] + list(itertools.accumulate(seq))

ans = 0
for left in range(0, len(b), 2):
    right = left + 2*k + 1
    if right >= len(b):
        right = len(b)-1

    ans = max(ans, b[right] - b[left])

print(ans)

Solution (Python):
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
  if s[n-1]=="0":
    li.append(0)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
  if s[n-1]=="0":
    li.append(0)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
if 2*k+1>=len_li:
  print(n)
elif k==0:
  import math
  o=math.ceil(len_li/2)
  le=[]
  for f in range (0,o):
    le.append(li[2*f])
    le.sort()
  print(le[len(le)-1])
else:
  import math
  u=0
  w=0
  while w<=len(lis)-2*k-2:
    d=lis[w+2*k+1]-lis[w]
    if u<=d:
      u=d
    w=w+2
  print(u)

Solution (Python):
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
  if s[n-1]=="0":
    li.append(0)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
  if s[n-1]=="0":
    li.append(0)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
if 2*k+1>=len_li:
  print(n)
elif k==0:
  import math
  o=math.ceil(len_li/2)
  le=[]
  for f in range (0,o):
    le.append(li[2*f])
    le.sort()
  print(le[len(le)-1])
else:
  import math
  u=0
  w=0
  while w<=len(lis)-2*k-2:
    d=lis[w+2*k+1]-lis[w]
    if u<=d:
      u=d
    w=w+2
  print(u)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    s = list(input())
    
    tank = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            tank.append(cnt)
            cnt = 0
        cnt += 1
        if i == n-1:
            tank.append(cnt)
            
    p = len(tank)
    
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()
        
    ans = 0
    res = 0
    if s[0] == '0':
        for j in range(2*k):
            res += tank[j]
        ans = res
        for j in range(2*k, p):
            if j == 2*k:
                res -= tank[0]
            elif j%2 == 0:
                res = res - tank[j-2*k] - tank[j-2*k-1]
            res += tank[j]
            ans = max(ans, res)
            
    else:
        for j in range(2*k+1):
            res += tank[j]
        ans = res
        for j in range(2*k+1, p):
            if j%2 == 1:
                res = res - tank[j-2*k-1] - tank[j-2*k]
            res += tank[j]
            ans = max(ans, res)
            
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline


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

    
    
    tank = []
    cnt = 1
    for i in range(1,n):
        if s[i] != s[i-1]:
            tank.append(cnt)
            cnt = 0
        cnt += 1
        if i == n-1:
            tank.append(cnt)
    
    p = len(tank)

    
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()

    
    ans = 0
    
    res = 0
    if s[0] == '0':
        for j in range(2*k):
            res += tank[j]
        ans = res
        for j in range(2*k,p):
            if j == 2*k:
                res -= tank[0]
            elif j%2 == 0:
                res= res - tank[j-2*k] -tank[j-2*k-1]
            res += tank[j]
            ans = max(ans,res)

    else:
        for j in range(2*k+1):
            res += tank[j]
        ans = res
        for j in range(2*k+1,p):
            if j % 2 == 1:
                res = res- tank[j-2*k-1]-tank[j-2*k]
            res += tank[j]
            ans = max(ans,res)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
s=input()
cnt=1
siten=s[0]
onezero=[0]
for i in range(n-1):
    if s[i]!=s[i+1]:
        onezero+=[cnt]
    cnt+=1

onezero+=[cnt]

x=len(onezero)

if siten=='1':
    onezero+=[onezero[-1]]*2
    t=int(x/2-1)
    if k>=t:
        print(n)
    else:
        ans=[0]
        for i in range(t-k+2):
            ans+=[onezero[2*(i+k)+1]-onezero[2*i]]
        print(max(ans))

else:
    onezero+=[onezero[-1]]
    t=int(x/2)
    if k>=t:
        print(n)
    else:
        ans=[onezero[2*k]]
        for i in range(t-k):
            ans+=[onezero[2*(i+k+1)]-onezero[2*i+1]]
        print(max(ans))

Solution (Python):
n,k=map(int,input().split())
s=input()
cnt=1
siten=s[0]
onezero=[0]
for i in range(n-1):
    if s[i]!=s[i+1]:
        onezero+=[cnt]
    cnt+=1

onezero+=[cnt]

x=len(onezero)

if siten=='1':
    onezero+=[onezero[-1]]*2
    t=int(x/2-1)
    if k>=t:
        print(n)
    else:
        ans=[0]
        for i in range(t-k+2):
            ans+=[onezero[2*(i+k)+1]-onezero[2*i]]
        print(max(ans))

else:
    onezero+=[onezero[-1]]
    t=int(x/2)
    if k>=t:
        print(n)
    else:
        ans=[onezero[2*k]]
        for i in range(t-k):
            ans+=[onezero[2*(i+k+1)]-onezero[2*i+1]]
        print(max(ans))

Solution (Python):
n,k = map(int,input().split())
s = input()
li = [0]
count = 1

for i in range(n-1):
    if s[i] != s[i+1]:
        li.append(count)
    count += 1
li.append(count)

m = len(li)

if s[0] == '1':
    li.append(li[-1])
    li.append(li[-1])
    t = int(m/2-1)
    if k >= t:
        print(n)
    else:
        max1 = 0
        for i in range(t-k+2):
            max1 = max(li[2*(i+k)+1] - li[2*i],max1)
        print(max1)
else:
    li.append(li[-1])
    t = int(m/2)
    if k >= t:
        print(n)
    else:
        max1 = li[2*k]
        for i in range(t-k):
            max1 = max(li[2*(i+k+1)] - li[2*i+1],max1)
        print(max1)

Solution (Python):
n,k=map(int,input().split())
s=input()
s_list=[]
first=s[0]
last=s[n-1]
state=first
length=1
for i in range(1,n):
    if (state=='0' and s[i]=='0') or (state=='1' and s[i]=='1'):
        state=s[i]
        length=length+1
    else:
        state=s[i]
        s_list.append(length)
        length=1
s_list.append(length)
m=len(s_list)
if first=='0' and last=='0':
    if (len(s_list)+1)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k]
        Sum=sum(p_list)
        Max=Sum
        i=1
        p_list=s_list[i:i+2*k+1]
        Sum=sum(p_list)
        if Sum>Max:
            Max=Sum
        i=i+2
        while i+2*k<m-1:
            Sum=Sum-s_list[i-2]-s_list[i-1]+s_list[i+2*k-1]+s_list[i+2*k]
            if Sum>Max:
                Max=Sum
            i=i+2
        p_list=s_list[m-2*k:]
        if sum(p_list)>Max:
            Max=sum(p_list)
elif first=='0' and last=='1':
    if len(s_list)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k]
        Sum=sum(p_list)
        Max=Sum
        i=1
        p_list=s_list[i:i+2*k+1]
        Sum=sum(p_list)
        if Sum>Max:
            Max=Sum
        i=i+2
        while i+2*k<=m-1:
            Sum=Sum-s_list[i-2]-s_list[i-1]+s_list[i+2*k-1]+s_list[i+2*k]
            if Sum>Max:
                Max=Sum
            i=i+2
elif first=='1' and last=='0':
    if len(s_list)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k+1]
        Sum=sum(p_list)
        Max=Sum
        i=2
        while i+2*k<m-1:
            Sum=Sum-s_list[i-2]-s_list[i-1]+s_list[i+2*k-1]+s_list[i+2*k]
            if Sum>Max:
                Max=Sum
            i=i+2
        p_list=s_list[m-2*k:]
        if sum(p_list)>Max:
            Max=sum(p_list)
elif first=='1' and last=='1':
    if (len(s_list)-1)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k+1]
        Sum=sum(p_list)
        Max=Sum
        i=2
        while i+2*k<=m-1:
            Sum=Sum-s_list[i-2]-s_list[i-1]+s_list[i+2*k-1]+s_list[i+2*k]
            if Sum>Max:
                Max=Sum
            i=i+2
print(Max)