Problem p03074 (python)

Fixed submission:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
if s[0] == "1" and s[x-1] == "0":
    s = s[::-1]
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
        ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    #s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
        ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
n,k = map(int,input().split())
s = list(input())
memory = s[0]
count = 1
lst = np.array([0])
M = 0
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        lst = np.append(lst,count)
        memory = s[i]
        count = 1
lst = np.append(lst,count)
cs = lst.cumsum()
if s[0] == '1':
    if (2*k+3) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 0
        while i+2*k+2 <= len(cs):           
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        print(M)
        exit()
if s[0] == '0':
    if (2*k+2) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 1
        while i+2*k+2 <= len(cs):
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        M = max(M,cs[2*k]-cs[0])
        print(M)
        exit()

Buggy submission (verdict: Runtime Error):
import numpy as np
n,k = map(int,input().split())
s = list(input())
memory = s[0]
count = 1
lst = np.array([0])
M = 0
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        lst = np.append(lst,count)
        memory = s[i]
        count = 1
lst = np.append(lst,count)
cs = lst.cumsum()
if s[0] == '1':
    if (2*k+3) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 0
        while i+2*k+2 <= len(cs):           
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        print(M)
        exit()
if s[0] == '0':
    if (2*k+2) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 1
        while i+2*k+2 <= len(cs):
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        M = max(M,cs[2*k]-cs[0])
        print(M)
        exit()

Buggy submission (verdict: Runtime Error):
import numpy as np
n,k = map(int,input().split())
s = list(input())
memory = s[0]
count = 1
lst = np.array([0])
M = 0
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        lst = np.append(lst,count)
        memory = s[i]
        count = 1
lst = np.append(lst,count)
cs = lst.cumsum()
if s[0] == '1':
    if 2*k+3 >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 0
        while i+2*k+2 <= len(cs):           
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        print(M)
        exit()
if s[0] == '0':
    if 2*k+2 >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 1
        while i+2*k+2 <= len(cs):
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        M = max(M,cs[2*k]-cs[0])
        print(M)
        exit()