Problem p03074 (python)

Fixed submission:
n,k = map(int,input().split())
s = input()
befores = '1'
cnt = 0
snum = []
for S in s:
	if S == befores:
		cnt += 1
	else:
		snum.append(cnt)
		cnt = 1
		befores = S
snum.append(cnt)
if s[-1] == '0':
    snum.append(0)
kazu = sum(snum[0:k * 2  + 1])
kaisuu = sum(snum[0:k * 2  + 1])
time = int((len(snum) - k * 2 - 1) / 2 )
one = 0
zero = 1
k = k * 2
for i in range(time):
	kazu = kazu - snum[one] - snum[zero] + snum[one + k + 2] + snum[zero + k]
	kaisuu = max(kaisuu,kazu)
	one += 2
	zero += 2
print(kaisuu)

Buggy submission (verdict: Runtime Error):
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 + 2])
		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))

Buggy submission (verdict: Runtime Error):
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 + 2])
		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))
print(AllLen)

Buggy submission (verdict: Runtime Error):
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 + 2])
		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 + 2])
		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))

Buggy submission (verdict: Runtime Error):
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))
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]) - len(S1[i]) + len(S0[i + k]) + 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':
	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]) - 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)
print(max(AllLen))