Problem p03074 (python)

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

count = 0
count_list = []
check = S[0]

for i in range(N):
    if S[i] == check:
         count += 1
    else:
        count_list.append(count)
        count = 1
        check = S[i]
    if i == N-1:
        count_list.append(count)

if S[0] == '0':
    count_list.insert(0, 0)
if S[-1] == '0':
    count_list.append(0)

k = min((len(count_list)-1)//2, K)

ans_list = []
for i in range((len(count_list)+1)//2 - k):
    if i == 0:
        tmp = sum(count_list[0:2*k+1])
    else:
        tmp = tmp - count_list[2*(i-1)] - count_list[(2*(i-1)+1)] 
        tmp = tmp + count_list[2*(k+i)-1] + count_list[2*(k+i)]
    ans_list.append(tmp)

print(max(ans_list))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = list(input())

count_list = []
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)


elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    



if len(count_list) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2



ans_list = [0] * m
if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:j+2*K+1])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:j+2*K+1])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:j+2*K])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:j+2*K])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:j+2*K]) 
        else:
            ans_list[i] = sum(count_list[j:j+2*K+1])

print(max(ans_list))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = list(input())

count_list = [0]*N
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list[i]= count
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list[i]= count
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list[i]= count
            elif check == 'a'and S[i] == '0':
                count_list[i]= count
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list[i]= count
                count = 1
                check = 'a'
                count_list[i]= count
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list[i]= count



elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    


for i in range(count_list.count(0)):
    count_list.remove(0)

if len(S) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2



ans_list = [0] * m
if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:j+2*K+1])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:j+2*K+1])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:j+2*K])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:j+2*K])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:j+2*K]) 
        else:
            ans_list[i] = sum(count_list[j:j+2*K+1])

print(max(ans_list))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = list(input())

count_list = []
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)


elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    



if len(S) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2



ans_list = [0] * m
if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:j+2*K+1])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:j+2*K+1])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:j+2*K])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:j+2*K])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:j+2*K]) 
        else:
            ans_list[i] = sum(count_list[j:j+2*K+1])

print(max(ans_list))