Problem p03074 (python)

Fixed submission:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0

for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
             count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            
        else:
            S_list.append(count)
            count = 1

S_list.append(count)
if S[-1] == "0":
    S_list.append(0)


now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)


answer_list = []
i = 0
if 2 * K + 1 < len(cum_list):
    while i + 2 * K + 1 < len(cum_list):
        answer_list.append(cum_list[i + 2 * K + 1] - cum_list[i])
        i += 2
else:
    answer_list.append(len(S))
answer = max(answer_list)
print(answer)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0

for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
             count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            
        else:
            S_list.append(count)
            count = 1

S_list.append(count)
if S[-1] == "0":
    S_list.append(0)


now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)


answer_list = []
i = 0
if 2 * K + 1 < len(cum_list):
    while i + 2 * K + 1 < len(cum_list):
        answer_list.append(cum_list[i + 2 * K + 1] - cum_list[i])
        i += 2
else:
    answer_list.append(len(S))
answer_list = check(cum_list,K)
answer = max(answer_list)
print(answer)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0

for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
             count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            
        else:
            S_list.append(count)
            count = 1

S_list.append(count)
if S[-1] == "0":
    S_list.append(0)


now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)


answer_list = []
i = 0
if 2 * K + 1 < len(x):
    while i + 2 * K + 1 < len(x):
        answer_list.append(x[i + 2 * K + 1] - x[i])
        i += 2
else:
    answer_list.append(len(S))
answer_list = check(cum_list,K)
answer = max(answer_list)
print(answer)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
if "0" not in S or "1" not in S:
    print(len(S))

else:
    for i in range(N):
        if i == 0:
            if int(S[i]) == tmp:
                count += 1
            else:
                S_list.append(count)
                count = 1
        else:
            if S[i] == S[i - 1]:
                count += 1
                if i == N - 1:
                    S_list.append(count)
            else:
                S_list.append(count)
                count = 1


    if S[-1] == "0":
        S_list.append(0)




    now = 0
    for i in range(len(S_list)):
        now += S_list[i]
        cum_list.append(now)
    def check(x,K):
        answer_list = []
        i = 0
        if i + 2 * K + 1 < len(x):
            while i + 2 * K + 1 < len(x):
                answer_list.append(x[i + 2 * K + 1] - x[i])
                i += 2
            return answer_list
        elif K > 0:
            check(x,K-1)
    answer_list = check(cum_list,K)

    answer = max(answer_list)
    print(answer)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
if "0" not in S:
    print(len(S))
else:
    for i in range(N):
        if i == 0:
            if int(S[i]) == tmp:
                count += 1
            else:
                S_list.append(count)
                count = 1
        else:
            if S[i] == S[i - 1]:
                count += 1
                if i == N - 1:
                    S_list.append(count)
            else:
                S_list.append(count)
                count = 1


    if S[-1] == "0":
        S_list.append(0)




    now = 0
    for i in range(len(S_list)):
        now += S_list[i]
        cum_list.append(now)
    def check(x,K):
        answer_list = []
        i = 0
        if i + 2 * K + 1 < len(x):
            while i + 2 * K + 1 < len(x):
                answer_list.append(x[i + 2 * K + 1] - x[i])
                i += 2
            return answer_list
        elif K > 0:
            check(x,K-1)
    answer_list = check(cum_list,K)

    answer = max(answer_list)
    print(answer)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
if S == "1":
    print(1)
else:
    for i in range(N):
        if i == 0:
            if int(S[i]) == tmp:
                count += 1
            else:
                S_list.append(count)
                count = 1
        else:
            if S[i] == S[i - 1]:
                count += 1
                if i == N - 1:
                    S_list.append(count)
            else:
                S_list.append(count)
                count = 1


    if S[-1] == "0":
        S_list.append(0)




    now = 0
    for i in range(len(S_list)):
        now += S_list[i]
        cum_list.append(now)
    def check(x,K):
        answer_list = []
        i = 0
        if i + 2 * K + 1 < len(x):
            while i + 2 * K + 1 < len(x):
                answer_list.append(x[i + 2 * K + 1] - x[i])
                i += 2
            return answer_list
        elif K > 0:
            check(x,K-1)
    answer_list = check(cum_list,K)

    answer = max(answer_list)
    print(answer)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
            count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            if i == N - 1:
                S_list.append(count)
        else:
            S_list.append(count)
            count = 1


if S[-1] == "0":
    S_list.append(0)




now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)
def check(x,K):
    answer_list = []
    i = 0
    if i + 2 * K + 1 < len(x):
        while i + 2 * K + 1 < len(x):
            answer_list.append(x[i + 2 * K + 1] - x[i])
            i += 2
        return answer_list
    elif K > 0:
        check(x,K-1)
        
    
    
answer_list = check(cum_list,K)
if answer_list == None:
    answer_list = [1]
answer = max(answer_list)
print(answer)