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.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left

def main():
	N, K = map(int, input().split(" "))
	S = input()
		
	S0 = S.split("0")
	S1 = S.split("1")
	
	S0 = [x for x in S0 if x]
	S1 = [x for x in S1 if x]
	
	if K == 0:
		ans = 0
		for val in S0:
			tmp = len(val)
			if tmp > ans:
				ans = tmp
		print(ans)
		sys.exit()
		
	if K >= len(S1):
		print(len(S))
		sys.exit()
	
	fst = []
	snd = []
	guu = False
	kii = False
	if S[0] == "1":
		fst = S0
		snd = S1
		kii = True
	else:
		fst = S1
		snd = S0
		guu = True
	
	ansList = []
	while True:
		if len(fst) != 0:
			ansList.append(fst.pop(0))
		if len(snd) != 0:
			ansList.append(snd.pop(0))
		if len(fst) == 0 and len(snd) == 0:
			break
			
	total = []
	for i, val in enumerate(ansList):
		if i == 0:
			total.append(len(val))
			continue
		total.append(total[i-1] + len(val))
	
	ans = 0
	if kii == True:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * K]
			elif i % 2 == 1 and i+1+(K-1)*2 < len(total):
				tmp = total[i+1 + (K-1)*2] - total[i-1]
			elif i+K*2 < len(total):
				tmp = total[i + K*2] - total[i-1]
			
			if ans < tmp:
				ans = tmp
	else:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * (K-1) + 1]
			elif i % 2 == 1 and i + K*2 < len(total):
				tmp = total[i + K*2] - total[i-1]
				#print("{} - {}".format(total[i + K*2], total[i-1]))
			elif i+1+(K-1)*2 < len(total):
				tmp = total[i+1+(K-1)*2] - total[i-1]
			
			#print(tmp)
			if ans < tmp:
				ans = tmp
	
	print(ans)
		
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []

for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
right = 0
left = 0
g = 0
su = 0
if A[0] == 0:
    for i in range(len(s)):
        print(right)
        su +=s[right]
        if right%2==0:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==0:
                  g -=1
              left +=1
        a = max(a,su)
else:
    for i in range(len(s)):
        su +=s[right]
        if right%2==1:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==1:
                  g -=1
              left +=1
print(s)
print(left)
print(right)
print(a)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)
if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=0
i=len(a)-1
if i- m<0:
	print(n)
else:
	while i-m>=0:
		tmp=a[i]-a[i-m]
		print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		m=t-m
		i-=1
	if a[i]>_max:
		_max=a[i]
	print(_max)

Submission status: Wrong Answer

Solution code:

N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []

for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
right = 0
left = 0
g = 0
su = 0
if A[0] == 0:
    for i in range(len(s)):
        su +=s[right]
        if right%2==0:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==0:
                  g -=1
              left +=1
else:
    for i in range(len(s)):
        su +=s[right]
        if right%2==1:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==1:
                  g -=1
              left +=1

print(a)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

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

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        
N2 = np.count_nonzero(State)
State = State[:N2]

flag_0s = S[0] == '0'
flag_0e = S[len(S)-1] == '0'

ans = 0
total = 0
K2 = 2*K+1

if K2 >= N2:
    K2 = N2

for i in range(K2-int(flag_0s)):
    total += State[i]
        
ans = total
for i in range(N2-K2):
    if i == 0 and flag_0s:
        total2 = total - State[i] + State[i+K2-1] + State[i+K2]
    elif i == N2-K2-1 and flag_0e:
        total2 = total - State[i] + State[i+K2] - State[i+1]
    else:
        total2 = total - State[i] + State[i+K2]
    ans = max(ans,total2)
    total = total2
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)
if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=0
i=len(a)-1
if i-m<0:
	print(n)
else:
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		m=t-m
		i-=1
	if a[i]>_max:
		_max=tmp
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)
if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=0
i=len(a)-1
if i-m<0:
	print(n)
else:
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		m=t-m
		i-=1
	if i>=1 and a[i]>_max:
		_max=tmp
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)
if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=0
i=len(a)-1
if i-m<0:
	print(n)
else:
	while i-m>=0:
		tmp=a[i]-a[i-m]
		print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		m=t-m
		i-=1
	if i>=1 and a[i]>_max:
		_max=tmp
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=0
i=len(a)-1
if i-m<0:
	print(n)
else:
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		m=t-m
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
_max=0
i=len(a)-1
if i-m<0:
	print(n)
else:
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []

for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
right = 0
left = 0
g = 0
su = 0
if A[0] == 0:
    for i in range(len(s)):
        su +=s[right]
        if right%2==0:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==0:
                  g -=1
              left +=1
else:
    for i in range(len(s)):
        su +=s[right]
        if right%2==1:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==1:
                  g -=1
              left +=1

print(a)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left

def main():
	N, K = map(int, input().split(" "))
	S = input()
		
	S0 = S.split("0")
	S1 = S.split("1")
	
	S0 = [x for x in S0 if x]
	S1 = [x for x in S1 if x]
	
	if K == 0:
		ans = 0
		for val in S0:
			tmp = len(val)
			if tmp > ans:
				ans = tmp
		print(ans)
		sys.exit()
		
	if K >= len(S1):
		print(len(S))
		sys.exit()
	
	fst = []
	snd = []
	guu = False
	kii = False
	if S[0] == "1":
		fst = S0
		snd = S1
		kii = True
	else:
		fst = S1
		snd = S0
		guu = True
	
	ansList = []
	while True:
		if len(fst) != 0:
			ansList.append(fst.pop(0))
		if len(snd) != 0:
			ansList.append(snd.pop(0))
		if len(fst) == 0 and len(snd) == 0:
			break
			
	#ansList.reverse()
	#print(ansList)
	total = []
	for i, val in enumerate(ansList):
		if i == 0:
			total.append(len(val))
			continue
		total.append(total[i-1] + len(val))
		
	#print(guu)
	ans = 0
	if kii == True:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * K]
			elif i % 2 == 1 and i+1+(K-1)*2 < len(total):
				tmp = total[i+1 + (K-1)*2] - total[i-1]
			elif i+K*2 < len(total):
				tmp = total[i + K*2] - total[i-1]
			
			if ans < tmp:
				ans = tmp
	else:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * (K-1) + 1]
			elif i % 2 == 1 and i + K*2 < len(total):
				tmp = total[i + K*2] - total[i-1]
				#print("{} - {}".format(total[i + K*2], total[i-1]))
			elif i+1+(K-1)*2 < len(total):
				tmp = total[i+1+(K-1)*2] - total[i-1]
			
			if ans < tmp:
				ans = tmp
	
	print(ans)
		
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

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

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        
N2 = np.count_nonzero(State)
State = State[:N2]
ans = 0
K2 = 2*K+1
if K2 >= N2:
    K2 = N2        
    for i in range(K2):
        ans += State[i]
    print(ans)
    sys.exit()

for i in range(K2):
    ans += State[i]
    
for i in range(N2-K2):
    ans2 = ans - State[i]
    ans2 += State[i+K2]
    ans3 = max(ans,ans2)
    ans = ans2
    
print(ans3)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S= input()
i,j = 0,0
onenumber = [0] * (K+1)
zeronumber = [0] * K
test, answer = 0,0
infront = ''

for x in S:
    if x == '0':
        if infront == '1':
            test += onenumber[j]
            answer = max(answer,test)
            print(answer)
            if j == K:
                j = -1
            j += 1
            if i == K:
                i = 0
            test -= onenumber[j]
            test -= zeronumber[i]
            onenumber[j],zeronumber[i] = 0, 0
        zeronumber[i] += 1
        infront = '0'
    if x == '1':
        if infront == '0':
            test += zeronumber[i]
            i += 1
        onenumber[j] += 1
        infront = '1'

if S[-1] == '0':
    test += zeronumber[i]
else:
    test += onenumber[j]
answer = max(answer,test)
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left

def main():
	N, K = map(int, input().split(" "))
	S = input()
		
	S0 = S.split("0")
	S1 = S.split("1")
	
	S0 = [x for x in S0 if x]
	S1 = [x for x in S1 if x]
	
	if K == 0:
		ans = 0
		for val in S0:
			tmp = len(val)
			if tmp > ans:
				ans = tmp
		print(ans)
		sys.exit()
		
	if K >= len(S1):
		print(len(S))
		sys.exit()
	
	fst = []
	snd = []
	guu = False
	kii = False
	if S[0] == "1":
		fst = S0
		snd = S1
		kii = True
	else:
		fst = S1
		snd = S0
		guu = True
	
	ansList = []
	while True:
		if len(fst) != 0:
			ansList.append(fst.pop())
		if len(snd) != 0:
			ansList.append(snd.pop())
		if len(fst) == 0 and len(snd) == 0:
			break
			
	ansList.reverse()
	total = []
	for i, val in enumerate(ansList):
		if i == 0:
			total.append(len(val))
			continue
		total.append(total[i-1] + len(val))
		
	#print(guu)
	ans = 0
	if kii == True:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * K]
			elif i % 2 == 1 and i+1+(K-1)*2 < len(total):
				tmp = total[i+1 + (K-1)*2] - total[i-1]
			elif i+K*2 < len(total):
				tmp = total[i + K*2] - total[i-1]
			
			if ans < tmp:
				ans = tmp
	else:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * (K-1) + 1]
			elif i % 2 == 1 and i + K*2 < len(total):
				tmp = total[i + K*2] - total[i-1]
			elif i+1+(K-1)*2 < len(total):
				tmp = total[i+1+(K-1)*2] - total[i-1]
			
			if ans < tmp:
				ans = tmp
		
	print(ans)
		
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(lambda x: x == "1", list(input())))

change_flg = False
cnt = 0
ilands = []


def cumsum(array):
    value = array[0]
    results = [value] * len(array)
    for index, elm in enumerate(array[1:], 1):
        value += elm
        results[index] = value
    return results


def change():
    global cnt, change_flg
    ilands.append(cnt)
    cnt = 0
    change_flg = not change_flg

for value in s:
    if value: # case 1 cnt
        if change_flg:
            change()
    else: # case 0 cnt : plus
        if not change_flg:
            change()
    cnt += 1

ilands.append(cnt)

if not len(ilands) % 2:
    ilands.append(0)

merged = list(cumsum(ilands))
section = 2 * k + 1

results = [0] * (len(ilands) // 2 - (k - 1))
if len(results) < section:
    print(n)
    exit()

merged.append(0)
for i in range(len(results)):
    start = i * 2 - 1
    results[i] = merged[start + section] - merged[start]

print(max(results))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(lambda x: x == "1", list(input())))

change_flg = False
cnt = 0
ilands = []


def cumsum(array):
    value = array[0]
    results = [value] * len(array)
    for index, elm in enumerate(array[1:], 1):
        value += elm
        results[index] = value
    return results


def change():
    global cnt, change_flg
    ilands.append(cnt)
    cnt = 0
    change_flg = not change_flg

for value in s:
    if value: # case 1 cnt
        if change_flg:
            change()
    else: # case 0 cnt : plus
        if not change_flg:
            change()
    cnt += 1

ilands.append(cnt)

ilands += [0] * 10**5

if not len(ilands) % 2:
    ilands.append(0)

merged = list(cumsum(ilands))
section = 2 * k + 1

results = [0] * (len(ilands) // 2 - (k - 1))

merged.append(0)
for i in range(len(results)):
    start = i * 2 - 1
    results[i] = merged[start + section] - merged[start]

# print(max(results))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(lambda x: x == "1", list(input())))

change_flg = False
cnt = 0
ilands = []


def cumsum(array):
    value = array[0]
    results = [value] * len(array)
    for index, elm in enumerate(array[1:], 1):
        value += elm
        results[index] = value
    return results


def change():
    global cnt, change_flg
    ilands.append(cnt)
    cnt = 0
    change_flg = not change_flg

for value in s:
    if value: # case 1 cnt
        if change_flg:
            change()
    else: # case 0 cnt : plus
        if not change_flg:
            change()
    cnt += 1

ilands.append(cnt)

ilands += [0] * 10**5

if not len(ilands) % 2:
    ilands.append(0)

merged = list(cumsum(ilands))
section = 2 * k + 1

results = [0] * (len(ilands) // 2 - (k - 1))

# merged.append(0)
# for i in range(len(results)):
#     start = i * 2 - 1
#     results[i] = merged[start + section] - merged[start]
# 
# print(max(results))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
if n<m or len(a)-m-1<0:
	print(n)
elif len(a)==2 and s[0]=='1':
	if k==0:
		print(0)
	else:
		print(n)
else:
	_max=0
	i=len(a)-1
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
if n<m or len(a)-m-1<0:
	print(n)
elif len(a)==2 and s[0]=='1':
	if k==0:
		print(0)
	else:
		print(n)
else:
	_max=0
	i=len(a)-1
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
if n<m or len(a)==2 or len(a)-m-1<0:
	print(n)
else:
	_max=0
	i=len(a)-1
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
if n<m or len(a)==2 or len(a)-m-1<0:
	print(n)
else:
	_max=0
	i=len(a)-1
	while i-m>=0:
		tmp=a[i]-a[i-m]
		print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)


if s[-1]=='0':
	m=2*k
else:
	m=2*k+1
if n<m or len(a)==2:
	print(n)
else:
	_max=0
	i=len(a)-1
	while i-m>=0:
		tmp=a[i]-a[i-m]
		#print('i:{} tmp:{}'.format(i,tmp))
		if tmp>_max:
			_max=tmp
		i-=1
	print(_max)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left

def main():
	N, K = map(int, input().split(" "))
	S = input()
		
	S0 = S.split("0")
	S1 = S.split("1")
	
	S0 = [x for x in S0 if x]
	S1 = [x for x in S1 if x]
	
	fst = []
	snd = []
	fstflg = False
	sndflg = False
	if S[0] == "1":
		fst = S0
		snd = S1
		sndflg = True
	else:
		fst = S1
		snd = S0
		fstflg = True
	
	#print(fst)
	ans = 0
	for i in range(len(S0) + len(S1)):
		tmp = 0
		k = 0
		for j in range(len(S0) + len(S1)):
			if len(S0) + len(S1) -1 <= i + j:
				break
			
			if i+j == 0:
				if fstflg == True:
					if k == K:
						break
					k += 1
				tmp += len(fst[0])
				
			elif i+j % 2 == 1:
				if sndflg == True:
					if k == K:
						break
					k += 1
				aa = (i+j)//2
				tmp += len(snd[aa])
					
			else:
				if fstflg == True:
					if k == K:
						break
					k += 1
				aa = (i+j) // 2
				tmp += len(fst[aa])
		
		#print(tmp)
		if tmp > ans:
			ans = tmp
			
	print(ans)
		
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
tmp=1
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)

if len(a)==2: 
	print(a[1])
else:
	if s[-1]=='0':
		m=2*k
	else:
		m=2*k+1
	if len(s)<m:
		print(len(s))
	else:
		_max=0
		i=len(a)-1
		while i-m>=0:
			tmp=a[i]-a[i-m]
			#print('i:{} tmp:{}'.format(i,tmp))
			if tmp>_max:
				_max=tmp
			i-=1
		print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
a.append(0)
tmp=1
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)

if len(a)==2:
	print(a[1])
else:
	if s[0]=='0':
		m=2*k
	else:
		m=2*k+1
	if m>len(s):
		print(len(s))
	else:
		_max=0
		i=1
		while m+i<len(a):
			tmp=a[i+m]-a[i]
			#print(tmp)
			if tmp>_max:
				_max=tmp
			i+=1
		print(_max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
cnt = [0]
flag = False
for c in S:
    if c == '0':
        if flag:
            cnt.append(cnt[-1])
        else:
            flag = True
            cnt.append(cnt[-1] + 1)
    else:
        cnt.append(cnt[-1])
        flag = False
ans = 0
left, right = 0, 0
while right < N + 1:
    if cnt[right] - cnt[left] <= K:
        right += 1
        ans = max(ans, right - left)
    elif right == left:
        right += 1
        left += 1
    else:
        ans = max(ans, right - left)
        left += 1
print(ans - 1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
cnt = [0]
flag = False
for c in S:
    if c == '0':
        if flag:
            cnt.append(cnt[-1])
        else:
            flag = True
            cnt.append(cnt[-1] + 1)
    else:
        cnt.append(cnt[-1])
        flag = False
ans = 0
left, right = 0, 0
while right < N:
    if cnt[right + 1] - cnt[left] <= K:
        right += 1
        ans = max(ans, right - left)
    elif right == left:
        right += 1
        left += 1
    else:
        ans = max(ans, right - left)
        left += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9) 
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
def serch_right(x,K,t):
    global count0,count1
    if x<0 or len(A)<=x:
        return count1
    if A[x]==0:
        if t == 0:
            t=0
            count1 +=1
        elif t==1 :
            if count0==K:
                return count1
            t=0
            count1 +=1
            count0 +=1

    else:
        t=1
        count1+=1
    count1 = serch_right(x+1,K,t)
    return count1
def serch_left(x,A):
    global count2
    if x<0 or len(A)<=x or A[x]==0:
        return count2
    if A[x]==1:
        count2 +=1
    count2 = serch_left(x-1,A)     
    return count2
count =0
count0 =0
count1 =0
for i in range(len(A)):
    if A[i]==0 and (A[i-1]!=0 or i == 0):
        count2 = 0
        count1 =0
        count0 =0
        t = 1
        count1 = serch_right(i,K,t)
        count2 = serch_left(i-1,A)
        count1 = count1 + count2
        count = max(count1,count)
print(max(count,1))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
tmp=1
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		a.append(i+1)
a.append(n)

#print(a)

if s[0]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=a[0]
for i in range(len(a)):
	tmp=sum(a[i:i+m])
	if tmp>_max:
		_max=tmp
	m=t-m
print(_max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
tmp=1
for i in range(len(s)-1):
	if s[i] == s[i+1]:
		tmp+=1
	else:
		a.append(tmp)
		tmp=1
a.append(tmp)
if s[0]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=a[0]
for i in range(len(a)):
	tmp=sum(a[i:i+m])
	if tmp>_max:
		_max=tmp
print(_max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = str(input())
Sb = []
zero = 0
for i in range(n):
  if int(s[i]):
    if len(Sb) == 0 or Sb[-1][0] == 0:
      Sb.append([1, 1])
    else:
      Sb[-1][1] += 1
  else:
    if len(Sb) == 0 or Sb[-1][0]:
      Sb.append([0, 1])
      zero += 1
    else:
      Sb[-1][1] += 1
if zero <= k:
  print(n)
else:
  start = 0
  if Sb[0][0]:
    start = 1
  ans = sum([Sb[i][1] for i in range(start + 2 * k)])
  tmp = ans
  for i in range(zero - k):
    if i != zero - k - 1:
      tmp += Sb[start + 2 * k - 1 + i + 2][1] + Sb[start + 2 * k - 1 + i + 1][1] - Sb[start + i][1] - Sb[start + i + 1][1]
    else:
      tmp += Sb[start + 2 * k - 1 + i + 1][1] - Sb[start + i][1] - Sb[start + i + 1][1]
    ans = max(ans, tmp)
  print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
tmp=1
for i in range(len(s)-1):
	if s[i] == s[i+1]:
		tmp+=1
	else:
		a.append(tmp)
		tmp=1
a.append(tmp)

b=[]
b.append(a[0])
if s[0]=='0':
	m=2*k
else:
	m=2*k+1
for i in range(len(a)-k):
	b.append(sum(a[i:i+m]))
print(max(b))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split()) #最大2k+1個の区間を連結できる
s = list(input())
a = []
counter = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        counter += 1
    else:
        a.append(counter)
        counter = 1
a.append(counter)
m = len(a)
cum = [0]
for i in range(m):
    cum.append(cum[i]+a[i])

ans = 0
if m-2*k <= 0:
    ans = cum[-1] -cum[0]
for i in range(m):
    if ans < cum[min(m,i+2*k+1)] - cum[i]:
        ans = cum[min(m,i+2*k+1)] - cum[i]
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split()) #最大2k+1個の区間を連結できる
s = list(input())
a = []
counter = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        counter += 1
    else:
        a.append(counter)
        counter = 1
a.append(counter)
m = len(a)
cum = [0]
for i in range(m):
    cum.append(cum[i]+a[i])

ans = 0
if m-2*k <= 0:
    ans = cum[-1] -cum[0]
for i in range(m):
    if i == 0:
        if ans < cum[min(m,i+2*k)] - cum[i]:
            ans = cum[min(m,i+2*k)] - cum[i]
    else:
        if ans < cum[min(m,i+2*k+1)] - cum[i]:
            ans = cum[min(m,i+2*k+1)] - cum[i]
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split()) #最大2k+1個の区間を連結できる
s = list(input())
a = []
counter = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        counter += 1
    else:
        a.append(counter)
        counter = 1
a.append(counter)
m = len(a)
cum = [0]
for i in range(m):
    cum.append(cum[i]+a[i])

ans = 0
if m-2*k <= 0:
    ans = cum[-1] -cum[0]
for i in range(m-2*k):
    if i == 0:
        if ans < cum[min(m,i+2*k)] - cum[i]:
            ans = cum[min(m,i+2*k)] - cum[i]
    else:
        if ans < cum[min(m,i+2*k+1)] - cum[i]:
            ans = cum[min(m,i+2*k+1)] - cum[i]
    
print(ans)

Submission status: Wrong Answer

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

a = []

before = s[0]
first = int(s[0])
count = 1
for x in s[1:]:
    if x != before:
        a.append(count)
        count = 1
    else:
        count += 1
    before = x
a.append(count)

if len(a) < k * 2:
    print(n)
    exit()

ans = 0
for i in range(len(a) - (k * 2) + 1):
    temp = 0
    mode = first
    x = k
    for p in range(i, len(a)):
        #print(i, mode, temp, x)
        if mode == 0:
            if x == 0:
                break
            x -= 1
        temp += a[p]
        p += 1
        mode = (mode + 1) % 2
    ans = max(temp, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
l = [0]
for i in range(n-1):
    if s[i+1] != s[i]:
        l.append(i+1)
l.append(n+1)

ans = 0
for i in range(len(l)-1):
    ind = min(i+2*k + int(s[i]=="1"), len(l) - 1)
    ans = max(ans, l[ind] - l[i])

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
l = [0]
for i in range(n-1):
    if s[i+1] != s[i]:
        l.append(i+1)
l.append(n)

ans = 0
for i in range(len(l)-1):
    ind = min(i+2*k + int(s[i]=="1"), len(l) - 1)
    ans = max(ans, l[ind] - l[i])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N,K=map(int,input().split())
s=input()

x=[]
tmp=0
for i in range(N):
    if s[i]=="1":
        if tmp!=0: x.append([tmp,i-1]) 
        tmp=0
    
    else: tmp+=1
if tmp!=0: x.append([tmp,i]) 

l=len(x)
if l<=1: print(N),sys.exit()

y=[ [0, x[1][1]-x[1][0] ] ]
for i in range(1,l-1):
    y.append([x[i-1][1]+1, x[i+1][1]-x[i+1][0] ])

y.append([x[l-2][1]+1 ,N-1])

ans=0
for i in range(l-K+1):
    ans=max(ans,y[i+K-1][1]-y[i][0]+1 )
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N,K = map(int, input().split())
S = list(map(int, input()))

groups = list()
now = 1
cnt = 0

for s in S:
    if s == now:
        cnt += 1
    else:
        groups.append(cnt)
        now ^= 1
        cnt = 1

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

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

cumsum = [0] + list(accumulate(groups))

Add = 2 * K + 1

ans = 0

for i in range(0,len(groups), 2):
    left = i
    right = min(i + Add, len(groups))
    tmp = cumsum[right] - cumsum[left]

    ans = max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N,K = map(int, input().split())
S = list(map(int, input()))

groups = list()
now = 1
cnt = 0

for s in S:
    if s == now:
        cnt += 1
    else:
        groups.append(cnt)
        now ^= 1
        cnt = 1

groups.append(cnt)

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

cumsum = [0] + list(accumulate(groups))

Add = 2 * K + 1

ans = 0

for i in range(0,len(groups), 2):
    left = i
    right = min(i + Add, len(groups))
    tmp = cumsum[right] - cumsum[left]

    ans = max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
t = list(str(input()))
t = [int(s) for s in t]
last = t[0]
temp_c = 0
s = []
con_c = 0
temp_con_c = 0
corrent_p = 0
sum_t = []
for i in range(n):
    if last == t[i]:
        temp_c += 1
    else:
        last = t[i]
        s.append(temp_c)
        temp_c = 1
s.append(temp_c)

l = len(s)
sum_t.append(0)
for i in range(l):
    sum_t.append(sum_t[i]+s[i])
    
ans = 0
Add = 2*k+1
for i in range(len(sum_t)):
    if i%2==0:
        left = i
        right = min(i+Add,len(sum_t)-1)
        tmp = sum_t[right]-sum_t[left]
        ans = max(tmp,ans)
    
print(ans)