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()
 
zeros = []
ones = [0]


 
prev = '1'
for c in S:
    if c == '1':
        if prev == '0':
            ones.append(1)
        else:
            ones[-1] += 1
    else: 
        if prev == '1':
            zeros.append(1)
        else:
            zeros[-1] += 1
    prev = c
ones.append(0)



tmp = sum(zeros[0:K]) + sum(ones[0:K+1])


ans = tmp
for i in range(0,len(zeros)-K):
    tmp += zeros[i+K] - zeros[i] + ones[i+K+1] - ones[i]
    ans = max(tmp,ans)

print(ans)

Solution (Python):
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N,K=na()
    S=input()
    l=[]
    N=len(S)
    
    if(S[0]=="0"):
        
        l.append(0)
    t=S[0]
    ct=1
    
    
    
    for b in S[1:]:
        
        if(t!=b):
            l.append(ct)
            ct=1
            t=b
        else:
            ct+=1
    
    l.append(ct)
    len_l=len(l)
    if(len_l//2 <= K):
        return sum(l)
    ans = res = sum(l[:2*K+1])
    
    if(len_l%2==0):
        l.append(0)
        len_l+=1
    g = K * 2 + 1
    for i in range(K*2+1,len_l-1,2):
        ans -= l[i-g] + l[i-g+1]
        ans += l[i] + l[1+i]
        if(res<ans):
            res=ans
    
    
    
    return res

print(Solve())

Solution (Python):
import sys
from itertools import accumulate
 
def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    S = str(input().strip())
    S += 'x'
 
    A = []
    S0 = S[0]
    cnt = 0
    for s in S:
        if S0 == s:
            cnt += 1
        else:
            A.append(cnt)
            cnt = 1
            S0 = s
 
 
    n = len(A) // 2
    if len(A) % 2 == 1 and S[0] == '0':
        n += 1
 
    if n <= K:
        return sum(A)
 
    if S[0] == '0':
        A = [0] + A
    if S[-2] == '0':
        A = A + [0]
    A = [0] + list(accumulate(A))
 
    ans = 0
    for i in range(0, len(A) - 2*K, 2):
        ans = max(ans, A[i + 2*K + 1] - A[i])
 
    return ans
 
 
if __name__ == '__main__':
    print(main())

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

for i in range(len(S)):
	if i==0:
		if S[0]=="0":
			line.append(0)
		line.append(1)
	else:
		if S[i-1]!=S[i]:
			line.append(1)
		else:
			line[-1]+=1
c=0
for i in range(len(line)):

	if i==0:
		if i+2*K+1<=len(line):
			s=sum(line[i:2*K+1])
			c=s
		else:
			s=sum(line[i:])
			c=s
	elif i%2==0:
		if i+2*K+1<=len(line):
			s=s-line[i-2]-line[i-1]+line[i+2*K-1]+line[i+2*K]
			c=max(c,s)
		else:
			c=max(c,sum(line[i:]))
			break
print(c)

Solution (Python):
from itertools import accumulate

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




nums = list()
now = 1
count_tmp = 0
for i in range(N):
    if S[i] == now:
        count_tmp += 1
    else:
        nums.append(count_tmp)
        now = 1 - now
        count_tmp = 1
if count_tmp > 0:
    nums.append(count_tmp)


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



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


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

Solution (Python):
import math
n, k = map(int, input().split())
S = str(input())

L = [0]

tmp = "1"

seq = 0
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 math.ceil(M/2) <= k:
    
    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-k*2, 2):
    ans = max(ans, X[i+k*2+1]-X[i])
print(ans)

"""
最初が0か1かで奇数番目を使うか偶数番目を使うか変わる
"""

"""
if k >= len(combined):
    print(len(S))
    exit()
初めて書いた時もこんなこと書いてる。

"""

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

left = 0
right = 0
ans = 0
remain = K
S += "?"
if S[0] == "0":
    remain -= 1

while left < N:
    while right < N and remain >= 0:
        if S[right] == "1" and S[right + 1] == "0":
            remain -= 1
        right += 1
    ans = max(ans, right - left)
    if left == right:
        right += 1
    else:
        if S[left] == "0" and S[left + 1] == "1":
            remain += 1
    left += 1
print(ans)

Solution (Python):
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N,K=na()
    S=[int(d) for d in input()]
    l=[]
    N=len(S)
    if(S[0]==0):
        l.append(0)
    b=S[0]
    ct=1
    for i in S[1:]:
        if(b!=i):
            l.append(ct)
            ct=1
            b=i
        else:
            ct+=1
    l.append(ct)
    len_l=len(l)
    if(len_l//2 <= K):
        return sum(l)
    ans = res = sum(l[:2*K+1])
    
    if(len_l%2==0):
        l.append(0)
        len_l+=1
    g = K * 2 + 1
    for i in range(K*2+1,len_l-1,2):
        ans -= l[i-g] + l[i-g+1]
        ans += l[i] + l[1+i]
        if(res<ans):
            res=ans
    
    
    
    return res

print(Solve())

Solution (Python):
import numpy as np

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

ones = [len(x) for x in s.replace('0', ' ').split()]
zeros = [len(x) for x in s.replace('1', ' ').split()]

if s[0]=='0':
  ones = [0] + ones
if s[-1]=='0':
  ones = ones + [0]
  
ones = np.array(ones)
zeros = np.array(zeros)

if k >= len(zeros):
  result = n
else: 
  result = [
    np.sum(ones[i:i+k+1]) + np.sum(zeros[i:i+k])
    for i in range(len(zeros)-k+1)]
  result = max(result)

print(result)

Solution (Python):
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)
    
one_sum = [0]*(len(one)+1)
zero_sum = [0]*(len(zero)+1)
for i in range(len(one)):
    one_sum[i+1] = one_sum[i]+one[i]
for i in range(len(zero)):
    zero_sum[i+1]=zero_sum[i]+zero[i]
    


if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:

            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        elif saigozero and i==len(zero)-k:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        else:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]
        ans = max(ans,tmp)
    print(ans)

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

X = [] 
Y = [0] 

prev = '1'
for c in S:
    if c == '1':
        if prev == '0':
            Y.append(1)
        else:
            Y[-1] += 1
    else: 
        if prev == '1':
            X.append(1)
        else:
            X[-1] += 1
    prev = c
Y.append(0)

tmp = sum(X[0:K]) + sum(Y[0:K+1])
ans = tmp
for i in range(0,len(X)-K):
    tmp += X[i+K] - X[i] + Y[i+K+1] - Y[i]
    ans = max(tmp,ans)

print(ans)

Solution (Python):

"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')


ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)


(x >> i) & 1


dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

"""

N, K = map(int, input().split())
S = input()
def main():
    now = 1
    cnt = 0
    nums = []
    for i in range(N):
        if S[i] == str(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
    res = 0
    
    sums = [0]*(len(nums)+1)
    for i in range(len(nums)):
        sums[i+1] = sums[i] + nums[i]
    for i in range(0, len(nums), 2):
        left = i
        right = min(i+add, len(nums))
        tmp = sums[right] - sums[left]
        res = max(tmp, res)
    """
    
    
    left = 0
    right = 0
    tmp= 0 
    for i in range(0, len(nums), 2):
        
        nextLeft = i
        nextRight = min(i+add, len(nums))
        
        while (nextLeft > left):
            tmp -= nums[left]
            left += 1
        
        
        while (nextRight > right):
            tmp += nums[right]
            right +=1
        res = max(tmp, res)
    """
    print(res)
 
if __name__ == '__main__':
    main()

Solution (Python):

"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')


ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)


(x >> i) & 1


dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

"""

N, K = map(int, input().split())
S = input()
def main():
    now = 1
    cnt = 0
    nums = []
    for i in range(N):
        if S[i] == str(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
    res = 0
    
    left = 0
    right = 0
    tmp= 0 
    for i in range(0, len(nums), 2):
        
        nextLeft = i
        nextRight = min(i+add, len(nums))
        
        while (nextLeft > left):
            tmp -= nums[left]
            left += 1
        
        
        while (nextRight > right):
            tmp += nums[right]
            right +=1
        res = max(tmp, res)
    print(res)
 
if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

n_sum = []

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

n_sum.append(ct)

if len(n_sum) == 1:
    print(ct)
    sys.exit()

else:
    if S[-1] == '0':
        n_sum.append(ct)
    if S[0] == '0':
        n_sum.insert(0, 0)

if K < len(n_sum)//2:
    Max = -1
    i = 0
    while i+2*K < len(n_sum):
        if i == 0:
            tmp = n_sum[i+2*K]
        else:
            tmp = n_sum[i+2*K] - n_sum[i-1]
        i += 2
        Max = max(Max, tmp)
else:
    Max = ct

print(Max)

Solution (Python):

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

I=[0]

for i in range(n-1):
    if s[i]!=s[i+1]:
        I.append(i+1)
        
r=len(I)

for _ in range(10**6):
        I.append(n)

X=[]

for i in range(r):
    if s[I[i]]=='0':
        X.append(I[i+2*k]-I[i])
    else:
        X.append(I[i+2*k+1]-I[i])

print(max(X))

Solution (Python):
from numpy import*
from itertools import*
N, K = map(int, input().split())
S=input()
zeros=[0]+[len(list(v)) for k,v in groupby(S) if k=="0"]
ones =[0]+(S[0]=="0")*[0]+[len(list(v)) for k,v in groupby(S) if k=="1"]+(S[-1]=="0")*[0]



c_zero= cumsum(zeros)
c_one = cumsum(ones)



if len(zeros) <= K:
    print(len(S))
else:
    ans = 0
    for i in range( len(zeros)-K ):
        aaa = c_zero[i+K]-c_zero[i] + c_one[i+K+1]-c_one[i]
        ans = max(aaa, ans)
    print(ans)

Solution (Python):
n,k=map(int,input().split())
s='1'+input()
i=j=0
while j<n:k-='10'==s[j:j+2];i+=k<0;k+=k<0and'01'==s[i:i+2];j+=1
print(n-i)

Solution (Python):








lis=list(map(int,input().split()))
S=input()

ans=0
a=[0]
b=[0]
i,flag=0,0
while i<lis[0]:
    j=1
    if S[i]=="0":
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="0":
                j+=1
            elif S[i+j]=="1":
                flag=1
        a.append(j+a[-1])
        i+=j
    else:
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="1":
                j+=1
            elif S[i+j]=="0":
                flag=1
        b.append(j+b[-1])
        i+=j
    flag=0

A=len(a)-1
B=len(b)-1

if A<=lis[1]:
    ans=lis[0]
else:
    if A>B:
        for i in range(A-lis[1]+1):
            if i==0:
                ans=a[lis[1]]+b[lis[1]]
            elif i>0 and i<A-lis[1]:
                tmp=a[i+lis[1]]-a[i]+b[i+lis[1]]-b[i-1]
                if ans<tmp:
                    ans=tmp
            elif i==A-lis[1]:
                tmp=a[-1]-a[-1-lis[1]]+b[-1]-b[-1-lis[1]]
                if ans<tmp:
                    ans=tmp
    elif A<B:
        for i in range(A-lis[1]+1):
            tmp=a[i+lis[1]]-a[i]+b[i+lis[1]+1]-b[i]
            if ans<tmp:
                ans=tmp
    else:
        if S[0]=="0":
            for i in range(A-lis[1]+1):
                if i==0:
                    ans=a[lis[1]]+b[lis[1]]
                else:
                    tmp=a[i+lis[1]]-a[i]+b[i+lis[1]]-b[i-1]
                    if ans<tmp:
                        ans=tmp
        else:
            for i in range(A-lis[1]+1):
                if i<A-lis[1]:
                    tmp=a[i+lis[1]]-a[i]+b[i+lis[1]+1]-b[i]
                    if ans<tmp:
                        ans=tmp
                else:
                    tmp=a[-1]-a[-1-lis[1]]+b[-1]-b[-1-lis[1]]
                    if ans<tmp:
                        ans=tmp
print(ans)

Solution (Python):
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
left=[0]
right=[0]
tmp=0
if s[0]==0:
  flag=False
  if len(stack)%2==1:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
else:
  if len(stack)%2==0:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      right.append(tmp)
    else:
      left.append(tmp)
  else:
    if flag:
      left.append(tmp)
    else:
      right.append(tmp)
ans=0
if flag:
    for i in range(len(right)-k-1):
      ans=max(ans,right[i+k+1]-left[i])
else:
  for i in range(len(right)-k):
    ans=max(ans,right[i+k]-left[i])
print(ans)

Solution (Python):
from collections import deque


def calc():
    q.appendleft(0)
    ans = 0
    d = '1'
    kc = 0
    for i, c in enumerate(S):
        if c != d:
            d = c
            if c == '0':
                if kc == K:
                    ans = max(ans, i - q.pop())
                else:
                    kc += 1
            else:
                q.appendleft(i)
    return max(ans, N - q.pop())


N, K = map(int, input().split())
S = input()
q = deque()
print(calc())

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,K=MAP()
S=input()

L=[]
cnt0=0

if S[0]=='0':
    L.append(0)
    cur=0
else:
    cur=1
cnt=0

for i in range(N):
    if cur==int(S[i]):
        cnt+=1
    else:
        L.append(cnt)
        if cur==0:
            cnt0+=1
        cnt=1
        
        cur=1-cur
else:
    if cnt!=0:
        L.append(cnt)
        if cur==0:
            cnt0+=1

if S[-1]=='0':
    L.append(0)

if cnt0<K:
    
    K=cnt0


l=0
r=K*2+1
ans=sm=sum(L[:K*2+1])
while l<len(L) and r<len(L):
    for i in range(2):
        sm+=L[r]
        r+=1
        sm-=L[l]
        l+=1
    ans=max(ans, sm)
print(ans)

Solution (Python):
from collections import deque


def nextj(n):
    while S[n] == '0':
        n += 1
        if n == N:
            return N
    q.appendleft(n)
    while S[n] == '1':
        n += 1
        if n == N:
            return N
    return n


def init(n):
    while S[n] == '1':
        n += 1
        if n == N:
            return N
    return n


def calc():
    q.appendleft(0)
    ans = 0
    j = init(0)
    if j == N:
        return N
    for _ in range(K - 1):
        j = nextj(j)
        if j == N:
            return N
    while True:
        j = nextj(j)
        ans = max(ans, j - q.pop())
        if j == N:
            return ans


N, K = map(int, input().split())
S = input()
q = deque()
print(calc())

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,K=MAP()
S=input()

L=[]
cnt0=0

if S[0]=='0':
    L.append(0)
    cur=0
else:
    cur=1
cnt=0

for i in range(N):
    if cur==int(S[i]):
        cnt+=1
    else:
        L.append(cnt)
        if cur==0:
            cnt0+=1
        cnt=1
        
        cur=1-cur
else:
    if cnt!=0:
        L.append(cnt)
        if cur==0:
            cnt0+=1

if S[-1]=='0':
    L.append(0)

acc=[0]+list(accumulate(L))
mx=0
if cnt0<K:
    
    K=cnt0
for i in range(0, len(acc), 2):
    if i+(K*2+1)<len(acc):
        
        mx=max(mx, acc[i+(K*2+1)]-acc[i])
print(mx)

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    
    
    c_num = "1"  
    cnt = 0
    seq = []

    for i in S:
        if i == c_num:
            cnt += 1
        else:
            c_num = i
            seq.append(cnt)
            cnt = 1
    seq.append(cnt)
    
    
    if len(seq) % 2 == 0:  
        seq.append(0)
    
    if len(seq) <= 2*K+1:  
        print(sum(seq))
        return

    answer = 0
    total = 0
    stpr = 0

    for j in range(len(seq)-2*K):
        while stpr < j+2*K+1:
            total += seq[stpr]
            stpr += 1
        if j % 2 == 0:  
            answer = max(answer, total)
        total -= seq[j]

    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    seq = []
    current = "1"
    cnt = 0
    
    
    for c in S:
        if c == current:
            cnt += 1
        else:
            seq.append(cnt)
            current = c
            cnt = 1
    seq.append(cnt)  
    if len(seq) % 2 == 0:
        seq.append(0)  

    
    
    if len(seq) <= K*2+1:
        print(sum(seq))
        return
    ans = 0
    total = 0
    j = 0
    
    

    for i in range(len(seq)-K*2):
        while j < i+K*2+1:
            total += seq[j]
            j += 1
        if i % 2 == 0:
            ans = max(ans, total)
        total -= seq[i]  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
s='1'+input()
i=0
for j in range(n):k-='10'==s[j:j+2];i+=k<0;k+=k<0and'01'==s[i:i+2]
print(n-i)

Solution (Python):
def solve():
    N,K=map(int,input().split())
    S=input()
    s2=[]
    cp='_'
    cn=0
    count={'0':0, '1':0}
    for c in S:
        if cp != c:
            if cp != '_':
                s2.append(cn)
                count[cp]+=1
            cp = c
            cn = 0
        cn += 1
    s2.append(cn)
    count[cp]+=1

    csum=[0,0,0,0,0]
    csumval=0
    for v in s2:
        csumval+=v
        csum.append(csumval)

    csum+=[csumval,csumval,csumval,csumval,csumval]
    s2_len = len(s2)
    
    
    csumlen=len(csum)

    if count['0'] <= K:
        return csum[-1]

    
    ans=0
    if S[0]=='1':
        for i in range(0,s2_len+3,2):
            if 5+i+K*2>=csumlen:
                break
            ans1=csum[5+i+K*2]-csum[5+i-1]
            
            ans=max(ans,ans1)
    else:
        for i in range(-1,s2_len+3,2):
            if 5+i+K*2>=csumlen:
                break
            ans1=csum[5+i+K*2]-csum[5+i-1]
            
            ans=max(ans,ans1)
    return ans

print(solve())

Solution (Python):
def solve():
    N,K=map(int,input().split())
    S=input()
    s2=[]
    cp='_'
    cn=0
    count={'0':0, '1':0}
    for c in S:
        if cp != c:
            if cp != '_':
                s2.append(cn)
                count[cp]+=1
            cp = c
            cn = 0
        cn += 1
    s2.append(cn)
    count[cp]+=1

    csum=[0,0,0,0,0]
    csumval=0
    for v in s2:
        csumval+=v
        csum.append(csumval)

    csum+=[csumval,csumval,csumval,csumval,csumval]
    s2_len = len(s2)
    
    
    csumlen=len(csum)

    if count['0'] <= K:
        return csum[-1]

    
    ans=0
    if S[0]=='1':
        for i in range(0,s2_len//2*2+3,2):
            if 5+i+K*2>=csumlen:
                ans1=csum[csumlen-1]-csum[5+i-1]
            else:
                ans1=csum[5+i+K*2]-csum[5+i-1]
            
            ans=max(ans,ans1)
    else:
        for i in range(-1,s2_len//2*2+3,2):
            if 5+i+K*2>=csumlen:
                ans1=csum[csumlen-1]-csum[5+i-1]
            else:
                ans1=csum[5+i+K*2]-csum[5+i-1]
            
            ans=max(ans,ans1)
    return ans

print(solve())

Solution (Python):
N,K=map(int, input().split())
instr=input()
lft=0
rgt=1
voids=int(instr[0] == '0')
maxlen=0
while lft < rgt:
    maxlen = max(maxlen, rgt-lft)
    if rgt < N and (instr[rgt] == '1' or (instr[rgt-1] == '0' and instr[rgt] == '0')):
        rgt += 1
    elif rgt < N and voids < K:
        voids += 1
        rgt += 1
    else:
        voids -= lft+1 < N and instr[lft] == '0' and instr[lft+1] == '1'
        lft += 1
print(maxlen)

Solution (Python):
N,K=map(int, input().split())
instr=input()
lft=0
rgt=1
voids=int(instr[0] == '0')
maxlen=0
while lft < rgt:
    maxlen = max(maxlen, rgt-lft)
    if rgt < N and (instr[rgt] == '1' or (instr[rgt-1] == '0' and instr[rgt] == '0')):
        rgt += 1
    elif rgt < N and voids < K:
        voids += 1
        rgt += 1
    else:
        voids -= lft+1 < N and instr[lft] == '0' and instr[lft+1] == '1'
        lft += 1
print(maxlen)

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

l = 0
count = 0

ans = 0
for r in range(N):
    if S[r] == '0':
        if r==0 or S[r-1]=='1':
            count += 1
    if count > K:
        while S[l] == '1':
            l += 1
        while S[l] == '0':
            l += 1
        count -= 1
    ans = max(ans, r-l+1)

print(ans)

Solution (Python):
t,s=open(0)
n,k=map(int,t.split())
i=0
for j in range(n):k-='1'>s[j]and'0'!=s[j-1];i+=k<0;k+=k<0and'1'>s[i-1]and'0'<s[i]
print(n-i)

Solution (Python):
def solve():
    N,K=map(int,input().split())
    S=input()
    s2=[]
    cp='1'
    cn=0
    count={'0':0, '1':0}
    for c in S:
        if cp != c:
            s2.append(cn)
            count[cp]+=1
            cp = c
            cn = 0
        cn += 1
    s2.append(cn)
    count[cp]+=1
    if cp=='0':
        s2.append(0)

    csum = [0]
    csumval = 0
    for v in s2:
        csumval += v
        csum.append(csumval)

    s2_len = len(s2)
    csum_len = len(csum)

    if count['0'] <= K:
        return csum[-1]

    
    ans=0
    for i in range(1,s2_len,2):
        ans1 = csum[min(i+K*2,s2_len)]-csum[i-1]
        
        ans = max(ans,ans1)
    return ans

print(solve())

Solution (Python):
t,s=open(0)
n,k=map(int,t.split())
i=0
for j in range(n):
 k-='1'>s[j]and'0'!=s[j-1]
 if k<0:k+='1'>s[i]and'0'<s[i+1];i+=1
print(n-i)

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

if "0" not in S:
	print len(S)
	quit()
L=[]
if S[0]=="0": L.append(0)
for i,x in enumerate(S):
	if i==0:
		cnt=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
	prev=x
else:
	L.append(cnt)

if S[-1]=="0": L.append(0)

LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)

K=min(K,len(L)/2)

ans=0
len_lsum=len(LSUM)
for l in range(0,len_lsum,2):
	r=l+2*K+1
	if r < len_lsum:
		ans=max(ans , LSUM[r]-LSUM[l])

print ans

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,k = LI()
    s = [int(c) for c in S()]
    a = []
    if s[0] == 0:
        a.append(0)
    t = s[0]
    c = 1
    for b in s[1:]:
        if t == b:
            c += 1
        else:
            a.append(c)
            c = 1
            t = b
    a.append(c)
    l = len(a)
    if l // 2 <= k:
        return sum(a)

    r = t = sum(a[:k*2+1])
    if l % 2 == 0:
        a.append(0)
        l += 1
    g = k * 2 + 1
    for i in range(k*2+1,l-1,2):
        t -= a[i-g] + a[i-g+1]
        t += a[i] + a[i+1]
        if r < t:
            r = t

    return r


print(main())

Solution (Python):
def main():
	N, K = map(int, input().split())
	S = input()
	current = "1"
	current_len = 0
	nums = []
	for i in range(N):
		if S[i] != current:
			nums.append(current_len)
			current_len = 1
			current = S[i]
		else:
			current_len += 1

	nums.append(current_len)
	if len(nums) %2 == 0:
		nums.append(0)

	distance = 2*K+1
	ans = 0
	sums = [0]*(len(nums)+1)
	for i in range(len(nums)):
		sums[i+1] = sums[i] + nums[i]

	for i in range(0, len(nums), 2):
		left = i
		right = min(i+distance, len(nums))
		tmp = sums[right] - sums[left]
		ans = max(tmp, ans)

	print(ans)


if __name__ == '__main__':
    main()

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

arr = []
cnt = 0
c = s[0]
for i in range(n):
    if c != s[i]:
        if c == '0':
            arr.append(-cnt)
        else:
            arr.append(+cnt)
        c = s[i]
        cnt = 1
    else:
        cnt += 1
if c == '0':
    arr.append(-cnt)
else:
    arr.append(+cnt)


if arr[0] < 0:
    arr = [0] + arr

s_arr = [0]
for val in arr:
    s_arr.append(s_arr[-1] + abs(val))

max_v = -1
m = len(arr)
for i in range(0, m, 2):
    max_v = max(max_v, s_arr[min(m, i+2*k+1)] - s_arr[i])
print(max_v)

Solution (Python):
def main():
	N, K = map(int, input().split())
	S = input()
	current = "1"
	current_len = 0
	nums = []
	for i in range(N):
		if S[i] != current:
			nums.append(current_len)
			current_len = 1
			current = S[i]
		else:
			current_len += 1

	nums.append(current_len)
	if len(nums) %2 == 0:
		nums.append(0)

	distance = 2*K+1
	ans = 0
	left, right = 0, 0
	tmp = 0
	for i in range(0, len(nums), 2):
		next_left = i
		next_right = min(i+distance, 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)


if __name__ == '__main__':
    main()