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 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

# 01文字列を配列に一文字ずつ格納
hito = list(s)

irekaeCnt = 0

listSakadachiCnt=[]

sakadachiCnt=0
tmpMaxCnt = 0
# 初期状態での連続逆立ち人数
for i in range(len(hito)):

    if hito[i] == "1":
        sakadachiCnt += 1
    else:
        if tmpMaxCnt < sakadachiCnt:
            tmpMaxCnt = sakadachiCnt
        sakadachiCnt = 0
    
if tmpMaxCnt < sakadachiCnt:
    tmpMaxCnt = sakadachiCnt

listSakadachiCnt.append(tmpMaxCnt)

sakadachiCnt=0
tmpMaxCnt = 0

# 0の出現箇所
listZeroFrom = []
listZeroTO = []
prevIsZero = False

# 0の開始終了位置
for i in range(len(hito)):
    if hito[i] == "0" and prevIsZero == False:
        listZeroFrom.append(i)
        prevIsZero = True
    elif hito[i] == "1" and prevIsZero == True:
        listZeroTO.append(i)
        prevIsZero = False
if prevIsZero==True:
    listZeroTO.append(len(hito)-1)
    prevIsZero = False


# n回まで入れ替え可能
for j in range(n):
    # 01文字列を配列に一文字ずつ格納
    hito = list(s)
    irekaeCnt = 0

    # if len(listZeroFrom) < j :
    #     for f in range(listZeroFrom[j],listZeroTO[j]):
    #         hito[f] = "1"
    #     irekaeCnt += 1
    #     j += 1

    for k in range(len(listZeroFrom)):
        # print("len(listZeroFrom):{},len(listZeroFrom):{},k:{}".format(len(listZeroFrom),len(listZeroTO),str(k)))
        for l in range(listZeroFrom[k],listZeroTO[k]):
            # print("hito[{}]:{}".format(l,hito[l]))
            hito[l] = "1"
            # print("hito[{}]:{}".format(l,hito[l]))
        irekaeCnt += 1

        # 連続逆立ち人数
        sakadachiCnt=0
        tmpMaxCnt = 0
        for m in range(len(hito)):
            # print("hito[{}]:{}".format(m,hito[m]))

            if hito[m] == "1":
                sakadachiCnt += 1
            else:
                if tmpMaxCnt < sakadachiCnt:
                    tmpMaxCnt = sakadachiCnt
                sakadachiCnt = 0
            
        if tmpMaxCnt < sakadachiCnt:
            tmpMaxCnt = sakadachiCnt

        listSakadachiCnt.append(tmpMaxCnt)

        if irekaeCnt == j :

            sakadachiCnt=0
            tmpMaxCnt = 0
            # 連続逆立ち人数
            for m in range(len(hito)):
                # print("hito[{}]:{}".format(m,hito[m]))

                if hito[m] == "1":
                    sakadachiCnt += 1
                else:
                    if tmpMaxCnt < sakadachiCnt:
                        tmpMaxCnt = sakadachiCnt
                    sakadachiCnt = 0
                
            if tmpMaxCnt < sakadachiCnt:
                tmpMaxCnt = sakadachiCnt

            listSakadachiCnt.append(tmpMaxCnt)

        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        irekaeCnt = 0

# for p in range(len(listSakadachiCnt)):
#     print(str(listSakadachiCnt[p]))

listSakadachiCnt.sort(reverse=True)

# 出力
print(str(listSakadachiCnt[0]))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1)&(len(po)>2*K+1):
		left=1
		right=2*K+1
		q=po[right]
	elif (S[0]=='1')&(right<1):
		ans=po[-1]
		break
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1)&(len(po)>2*K+1):
		left=1
		right=2*K+1
		q=po[right]
	elif (S[0]=='1')&(right<1):
		left=1
		right=2*K+1
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1)&(len(po)>2*K+1):
		q=po[2*K+1]
		left=1
		right=2*K+1
	elif (S[0]=='1')&(right<1):
		left=1
		right=2*K+1
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1):
		q=po[2*K+1]
		left=1
		right=2*K+3
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<1):
		q=po[2*K]+1
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	else:
		for i in range(N):
			for j in range(N):
				right+=1
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:

import sys

N, K = map(int, sys.stdin.readline().split())

S_origin = input()

countk = 0
start = 0
max = 0
for i in range(N):

    if S_origin[i] == "1" or (i > 0 and S_origin[i] == "0" and S_origin[i-1] =="0"):
        start += 1
        continue

    S = list(S_origin)
    process_flag = 0
    countk = 0#呪文のカウンタ
    for j in range(start,N):
        if K == countk:
            break
        if S[j] == "0":
            S[j] ="1"
            process_flag = 1
        elif process_flag == 1:
            countk +=1
            process_flag = 0

    #1が最大で何個並んでいるか
    count1 = 0
    current_max = 0
    for j in range(N):
        if S[j] == "1":
            count1 +=1
            if current_max < count1:
                current_max = count1
        else:
            count1 = 0
    if max < current_max:
        max = current_max
        print(max)
    start += 1

print(max)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<1):
		q=po[2*K]+1
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:

import sys

N, K = map(int, sys.stdin.readline().split())

S_origin = sys.stdin.readline()

countk = 0
start = 0
max = 0
for i in range(N):
    S = list(S_origin)

    if S_origin[i] == "1" or (i > 0 and S_origin[i] == "0" and S_origin[i-1] =="0"):
        start += 1
        continue

    process_flag = 0
    countk = 0#呪文のカウンタ
    for j in range(start,N):
        if K == countk:
            break
        if S[j] == "0":
            S[j] ="1"
            process_flag = 1
        elif process_flag == 1:
            countk +=1
            process_flag = 0

    #1が最大で何個並んでいるか
    count1 = 0
    current_max = 0
    for j in range(N):
        if S[j] == "1":
            count1 +=1
            if max < count1:
                max = count1
        else:
            count1 = 0
    if max < current_max:
        max = current_max
        print(max)
    start += 1

print(max)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<1):
		q=po[2*K]
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	else:
		print(po[1000000])
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<1):
		q=po[2*K]
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<1):
		q=po[2*K]
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans=0
left,right=0,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<=1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<=1):
		q=po[2*K+1]
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans=0
left,right=0,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)-K+2):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<=1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<=1):
		q=po[2*K+1]
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

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

siten=int(s[0])
memo=s[0]
onezero=[]
for i in range(n):
    if memo=='0':
        if s[i]=='0':
            cnt+=1
            if i==n-1:
                onezero+=[cnt]
        else:
            onezero+=[cnt]
            cnt=1
            memo=s[i]
            if i==n-1:
                onezero+=[cnt]
    else:
        if s[i]=='1':
            cnt+=1
            if i==n-1:
                onezero+=[cnt]
        else:
            onezero+=[cnt]
            cnt=1
            memo=s[i]
            if i==n-1:
                onezero+=[cnt]

ans=[sum(onezero[0:2])]

NN=len(onezero)-k*2

for i in range(NN):
    if siten%2!=0:
        ans+=[sum(onezero[i:i+k*2+1])]
        siten+=1
    else:
        siten+=1

print(max(ans))

Submission status: Wrong Answer

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

siten=int(s[0])
memo=s[0]
onezero=[]
for i in range(n):
    if memo=='0':
        if s[i]=='0':
            cnt+=1
            if i==n-1:
                onezero+=[cnt]
        else:
            onezero+=[cnt]
            cnt=1
            memo=s[i]
            if i==n-1:
                onezero+=[cnt]
    else:
        if s[i]=='1':
            cnt+=1
            if i==n-1:
                onezero+=[cnt]
        else:
            onezero+=[cnt]
            cnt=1
            memo=s[i]
            if i==n-1:
                onezero+=[cnt]

ans=[1]

NN=len(onezero)-k*2

for i in range(NN):

    ans+=[sum(onezero[i:i+k*2+1])]


print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
p=0
ans=0
left,right=1,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
right=2*K+1
for i in range(len(po)-K+2):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(left<=1):
		q=po[2*K]
		right=2*K
		left+=1
	elif right<len(po):
		q=po[right]-po[left]
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
p=0
ans=0
left,right=1,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
right=2*K+1
for i in range(len(po)-K+2):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(left<=1):
		q=po[2*K]
		right=2*K
		left+=1
	elif right<len(po):
		q=po[right]-po[left]
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K+1
		q=po[right]-po[left]
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,k = map(int,input().split())
S = input()
lenth = 0
AllLen =[]
preS0,preS1 = S.split('1'),S.split('0')
S0 = [i for i in preS0 if i != '']
S1 = [i for i in preS1 if i != '']
num0 = k
if len(S0) <= k:
	print(len(S))
	exit() 
if S[0] == '0':
	J = k
else:J = k + 1
FirstLen = sum([len(S0[i]) for i in range(k)]) + sum([len(S1[j]) for j in range(J)])
AllLen.append(FirstLen)
CheckLen = FirstLen
if S[0] == '1' and S[-1] == '1':
    for i in range(len(S0) - k):
	    CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
	    AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '1':
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
if S[0] == '1' and S[-1] == '0':
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '0':
	if len(S1) == 1:
		print(len(max(S0)) + len(S1[0]))
		exit()
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 2):
		CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
print(max(AllLen))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(input())
s = list((map(int, s)))
tipS = []
cnt = 1
ans = 0
zeroInd = []
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        tipS.append([s[i], cnt])
        cnt = 1
tipS.append([s[-1], cnt])
total_sum = [0] * len(tipS)
total_sum[0] += tipS[0][1]
for i in range(len(tipS)):
    if tipS[i][0] == 0:
        zeroInd.append(i)
    if i != 0:
        total_sum[i] += tipS[i][1] + total_sum[i-1]
if len(zeroInd) != 0:
    start = zeroInd[0]
tmp_cnt = 0
tmp = 0
tmp2 = 0
#print(total_sum)
for i in range(len(tipS)):

    if tipS[i][0] == 0:
        tmp_cnt += 1


        if tmp_cnt > k:
            ans = max(tmp, ans)
            #print("ans: {}".format(ans))
            tmp -= total_sum[start]
            #print("minus{}".format(tmp))
            tmp += tipS[i][1]
            tmp_cnt -= 1
            tmp2 += 1
            start = zeroInd[tmp2]

        else:
            tmp += tipS[i][1]


    else:
        tmp += tipS[i][1]
    #print("tmp: {}".format(tmp))

ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(input())
s = list((map(int, s)))
tipS = []
cnt = 1
ans = 0
zeroInd = []
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        tipS.append([s[i], cnt])
        cnt = 1
tipS.append([s[-1], cnt])
total_sum = [0] * len(tipS)
total_sum[0] += tipS[0][1]
for i in range(len(tipS)):
    if tipS[i][0] == 0:
        zeroInd.append(i)
    if i != 0:
        total_sum[i] += total_sum[i-1]
if len(zeroInd) != 0:
    start = zeroInd[0]
tmp_cnt = 0
tmp = 0
tmp2 = 0
for i in range(len(tipS)):

    if tipS[i][0] == 0:
        tmp_cnt += 1


        if tmp_cnt > k:
            ans = max(tmp, ans)
            #print(ans)
            tmp -= total_sum[start]
            #print("minus{}".format(tmp))
            tmp += tipS[i][1]
            tmp_cnt -= 1
            tmp2 += 1
            start = zeroInd[tmp2]

        else:
            tmp += tipS[i][1]


    else:
        tmp += tipS[i][1]

ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

L = [0]
R = []
for i in range(0, len(S) - 1):
    if S[i:i + 2] == "01":
        L.append(i + 1)
j = 0
while j < len(S) and S[j] == '1':
    j += 1
for i in range(j, len(S) - 1):
    if S[i:i + 2] == "10":
        R.append(i)
R.append(len(S) - 1)

ans = 0
z = min(len(L) - K, len(R) - K)
for i in range(z + 1):
    Ri = max(0, i + K - 1)
    d = R[Ri] - L[i] + 1
    # print("(l,r)=({},{}) : {}".format(L[i], R[Ri], d))
    ans = max(ans, d)

# print(L)
# print(R)

print(ans)

Submission status: Wrong Answer

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

S = input()

current = None
accum_value = 0
accum = []

one_values = [0]
for c in S:
    if current != c:
        if current != None:
            accum.append(accum_value)
            if current == '1':
                one_values.append(accum_value)
            accum_value = 0

    current = c
    accum_value +=1 

accum.append(accum_value)
if c == '1':
    one_values.append(accum_value)

def max_window(l, window_size):
    window_size = min(window_size, len(l))
    window_sum = sum(l[:window_size])
    max_sum = window_sum
    for i in range(window_size, len(l)):
        window_sum += l[i]
        window_sum -= l[i-window_size]
        max_sum = max(window_sum, max_sum)

    #print(l, window_size, max_sum)
    return max_sum

if K == 0:
    print(max(one_values))
    sys.exit(0)

ans = 0
if S[0] == '0' and S[-1] == '0':
    ans = max(max_window(accum, 2*K), max_window(accum[::-1], 2*K))
elif S[0] == '0' and S[-1] == '1':
    ans = max(max_window(accum, 2*K), max_window(accum[::-1], 2*K+1))
elif S[0] == '1' and S[-1] == '0':
    ans = max(max_window(accum, 2*K+1), max_window(accum[::-1], 2*K))
elif S[0] == '1' and S[-1] == '1':
    ans = max_window(accum, 2*K+1)
print(ans)

Submission status: Wrong Answer

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

S = input()

current = None
accum_value = 0
accum = []

one_values = [0]
for c in S:
    if current != c:
        if current != None:
            accum.append(accum_value)
            if current == '1':
                one_values.append(accum_value)
            accum_value = 0

    current = c
    accum_value +=1 

accum.append(accum_value)
if c == '1':
    one_values.append(accum_value)

def max_window(l, window_size):
    window_size = min(window_size, len(l))
    window_sum = sum(l[:window_size])
    max_sum = window_sum
    for i in range(window_size, len(l)):
        window_sum += l[i]
        window_sum -= l[i-window_size]
        max_sum = max(window_sum, max_sum)

    #print(l, window_size, max_sum)
    return max_sum

if K == 0:
    print(max(one_values))
    sys.exit(0)

ans = 0
if S[0] == '0' or S[-1] == '0':
    if S[0] == '0':
        ans = max(max_window(accum, 2*K), max_window(accum[1:], 2*K+1))
    if S[-1] == '0':
        ans = max(ans, max_window(accum[::-1], 2*K))
else:
    ans = max_window(accum, 2*K+1)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
i = 1
left = 0
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

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

S = input()

current = None
accum_value = 0
accum = []

one_values = [0]
for c in S:
    if current != c:
        if current != None:
            accum.append(accum_value)
            if current == '1':
                one_values.append(accum_value)
            accum_value = 0

    current = c
    accum_value +=1 

accum.append(accum_value)
if c == '1':
    one_values.append(accum_value)

def max_window(l, window_size):
    window_size = min(window_size, len(l))
    window_sum = sum(l[:window_size])
    max_sum = window_sum
    for i in range(window_size, len(l)):
        window_sum += l[i]
        window_sum -= l[i-window_size]
        max_sum = max(window_sum, max_sum)

    #print(l, window_size, max_sum)
    return max_sum

if K == 0:
    print(max(one_values))
    sys.exit(0)

if S[0] == '0':
    print(max(max_window(accum, 2*K), max_window(accum[1:], 2*K+1)))
else:
    print(max_window(accum, 2*K+1))

Submission status: Wrong Answer

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

S = input()

current = None
accum_value = 0
accum = []

one_values = [0]
for c in S:
    if current != c:
        if current != None:
            accum.append(accum_value)
            if current == '1':
                one_values.append(accum_value)
            accum_value = 0

    current = c
    accum_value +=1 

accum.append(accum_value)
if c == '1':
    one_values.append(accum_value)

def max_window(l, window_size):
    window_size = min(window_size, len(l))
    window_sum = sum(l[:window_size])
    max_sum = window_sum
    for i in range(window_size, len(l)):
        window_sum += l[i]
        window_sum -= l[i-window_size]
        max_sum = max(window_sum, max_sum)

    print(l, window_size, max_sum)
    return max_sum

if K == 0:
    print(max(one_values))
    sys.exit(0)

if S[0] == '0':
    print(max(max_window(accum, 2*K), max_window(accum[1:], 2*K+1)))
else:
    print(max_window(accum, 2*K+1))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
k += 1
s = "01" + input().rstrip()
ans = 0
i = 0
left = 2
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

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

S = input()

current = None
accum_value = 0
accum = []
for c in S:
    if current != c:
        if current != None:
            accum.append(accum_value)
            accum_value = 0

    current = c
    accum_value +=1 
accum.append(accum_value)

window_size = min(2*K + int(S[0] != '0'), len(accum))

window_sum = sum(accum[:window_size])
max_sum = window_sum

for i in range(window_size, len(accum)):
    window_sum += accum[i]
    window_sum -= accum[i-window_size]
    max_sum = max(window_sum, max_sum)

print(max_sum)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
left = 0
right = -1
pos = s.find('10', 0)
if pos == -1:
  i = n
else:
  i = pos + 1

while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 0
i = 0
left = 0
right = 0
while i < n:
    if k > 0:
        pos = s.find('1', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('0', pos)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos - 1
                i = pos
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 0
i = 0
left = 0
right = 0
while i < n:
    if k > 0:
        pos = s.find('1', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('0', pos)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
i = 1
left = 0
right = 1
while i < n:
    if k > 0:
        pos = s.find('1', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('0', pos)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = tuple([int(x) for x in input().split(' ')])
S = input()

y = ''
tmp = [('', 0)] * (2*K+2)
ans = [0]
 
for i, x in enumerate(S+'2'):
    if x != y:
        tmp.append((x, i))
        if tmp[-2-2*K][0]=='0':
            ans.append(tmp[-2][1]-tmp[-2-2*K][1])
        else:
            ans.append(tmp[-1][1]-tmp[-2-2*K][1])
        y = x
 
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, K = tuple([int(x) for x in input().split(' ')])
S = input()
 
y = '2'
tmp = [0] * (2*K+1)
ans = [0]
 
for x in S+'2':
    if x == y:
        tmp[-1] += 1
    else:
        if y=='0':
            ans.append(sum(tmp[:-1]))
        else:
            ans.append(sum(tmp)) 
        y = x
        tmp = tmp[1:] + [1]
 
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, K = tuple([int(x) for x in input().split(' ')])
S = input()
 
y = '2'
tmp = [0] * 5
ans = [0]
 
for x in S+'2':
    if x == y:
        tmp[-1] += 1
    else:
        if y=='0':
            ans.append(sum(tmp[:-1]))
        else:
            ans.append(sum(tmp)) 
        y = x
        tmp = tmp[1:] + [1]
 
print(max(ans))