Problem p03074 (python)

Fixed submission:
n,k = list(map(int, input().split()))
s = input()
if (s.find('0') < 0) or (s.find('1') < 0):
	print(len(s))
	quit()
else:
	s += '0' if s[n-1] == '1' else '1'
c = []

j = 0
for i in range(1, n+1):
	if s[i-1] == s[i]:
		j += 1
	else:
		c.append([s[i-1], j+1])
		j = 0

for i in range(1, len(c)):
	c[i][1] += c[i-1][1]

if 2*k >= len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if c[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k-1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	res = max(res, c[-1][1] - c[(len(c)-1) - 2*k][1]) if c[-1][0] == '0' else max(res, c[-1][1] - c[(len(c)-1) - (2*k+1)][1])

	print(res)
	quit()

Buggy submission (verdict: Runtime Error):
n,k = list(map(int, input().split()))
s = input()
if s[n-1] == '1':
	s += '0'
else:
	s += '1'
c = []

j = 0
for i in range(1, n+1):
	if s[i-1] == s[i]:
		j += 1
	else:
		c.append([s[i-1], j+1])
		j = 0

for i in range(1, len(c)):
	c[i][1] += c[i-1][1]

if 2*k >= 2*len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	print(res)
	quit()

Buggy submission (verdict: Runtime Error):
n,k = list(map(int, input().split()))
s = input()
s += '0' if s[n-1] == '1' else '0'
c = []

j = 0
for i in range(1, n+1):
	if s[i-1] == s[i]:
		j += 1
	else:
		c.append([s[i-1], j+1])
		j = 0

for i in range(1, len(c)):
	c[i][1] += c[i-1][1]

if 2*k >= 2*len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	print(res)
	quit()

Buggy submission (verdict: Runtime Error):
n,k = list(map(int, input().split()))
s = input()
s += '0' if s[n-1] == '1' else '0'
c = []

j = 0
for i in range(1, n+1):
  if s[i-1] == s[i]:
    j += 1
  else:
    c.append([s[i-1], j+1])
    j = 0

for i in range(1, len(c)):
  c[i][1] += c[i-1][1]

if 2*k >= 2*len(c):
  print(c[-1][1])
  quit()
else:
  p = 0
  res = 0
  if c[0][0] == '0':
    res = c[(k-1)+1][1] if k == 1 else c[k+1][1]
    p += 1
  else:
    res = c[(k-1)+2][1] if k == 1 else c[k+2][1]
    p += 2

  while p+2*k < len(c):
    res = max(res, c[p+2*k][1] - c[p-1][1])
    p += 2

  print(res)
  quit()

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":

	n,k = list(map(int, input().split()))
	s = input()
	s += '0' if s[n-1] == '1' else '0'
	c = []

	j = 0
	for i in range(1, n+1):
		if s[i-1] == s[i]:
			j += 1
		else:
			c.append([s[i-1], j+1])
			j = 0

	for i in range(1, len(c)):
		c[i][1] += c[i-1][1]

	if 2*k >= 2*len(c):
		print(c[-1][1])
		quit()
	else:
		p = 0
		res = 0
		if c[0][0] == '0':
			res = c[(k-1)+1][1] if k == 1 else c[k+1][1]
			p += 1
		else:
			res = c[(k-1)+2][1] if k == 1 else c[k+2][1]
			p += 2

		while p+2*k < len(c):
			res = max(res, c[p+2*k][1] - c[p-1][1])
			p += 2

		print(res)
		quit()

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":

	n,k = list(map(int, input().split()))
	s = input()
	s += '0' if s[n-1] == '1' else '0'
	c = []

	j = 0
	for i in range(1, n+1):
		if s[i-1] == s[i]:
			j += 1
		else:
			c.append([s[i-1], j+1])
			j = 0

	for i in range(1, len(c)):
		c[i][1] += c[i-1][1]

	if 2*k >= 2*len(c):
		print(c[-1][1])
		quit()
	else:
		p = 0
		res = 0
		if s[0][0] == '0':
			res = c[(k-1)+1][1] if k == 1 else c[k+1][1]
			p += 1
		else:
			res = c[(k-1)+2][1] if k == 1 else c[k+2][1]
			p += 2

		while p+2*k < len(c):
			res = max(res, c[p+2*k][1] - c[p-1][1])
			p += 2

		print(res)
		quit()