Problem p03074 (python)

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

bl = []
if S[0] == "0":
    bl.append(0)
bl.append(0)
for i in range(1, N):
    if S[i-1] != S[i]:
        bl.append(i)
bl.append(N)
bl.append(N)

nbl = len(bl)
m = [bl[i+2 * K+1] - bl[i] 
     for i in range(nbl - 2*K-1) if i%2 == 0]
if len(m) == 0:
    print(N)
else:
    print(max(m))

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

tmp_count = 1
count_list = []
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "0":
        tmp_count += 1
    elif S[i] == "1" and S[i+1] == "1":
        tmp_count += 1
    elif S[i] == "0" and S[i+1] == "1":
        count_list.append(tmp_count)
        tmp_count = 1
    elif  S[i] == "1" and S[i+1] == "0":
        count_list.append(tmp_count)
        tmp_count = 1
count_list.append(tmp_count)

count_sakadati = 0
tmp_list = []
if S[0] == "0":
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(0, len(count_list)-1, 2):
            if j == 0:
                tmp_max = count_list[j] + count_list[j+1]
                tmp_max_num = j
            else:
                count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        if tmp_max_num == 0:
            del count_list[0]
            del count_list[1]
            count_list.insert(0, tmp_max)
        else:
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            count_list.insert(tmp_max_num-1, tmp_max)
else:
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(1, len(count_list)-1, 2):
            if count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max:
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        count_list.insert(tmp_max_num-1, tmp_max)
            
print(max(count_list))

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

tmp_count = 1
count_list = []
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "0":
        tmp_count += 1
    elif S[i] == "1" and S[i+1] == "1":
        tmp_count += 1
    elif S[i] == "0" and S[i+1] == "1":
        count_list.append(tmp_count)
        tmp_count = 1
    elif  S[i] == "1" and S[i+1] == "0":
        count_list.append(tmp_count)
        tmp_count = 1
count_list.append(tmp_count)

print("countlist =", count_list)

count_sakadati = 0
tmp_list = []
if S[0] == "0":
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(0, len(count_list)-1, 2):
            if j == 0:
                tmp_max = count_list[j] + count_list[j+1]
                tmp_max_num = j
            else:
                count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        if tmp_max_num == 0:
            del count_list[0]
            del count_list[1]
            count_list.insert(0, tmp_max)
        else:
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            count_list.insert(tmp_max_num-1, tmp_max)
else:
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(1, len(count_list)-1, 2):
            if count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max:
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        count_list.insert(tmp_max_num-1, tmp_max)
            
print(max(count_list))