Problem p03074 (python)

Fixed submission:
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]

if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
else:
    sumi = 0
    sumi = sum(memo[: k * 2 + b + 1])
    temp = [sumi]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(b, len(memo) - 2 * k  - 1, 2):
        last = 0
        if i + 2 * k + 2 <= len(memo) - 1:
            last = memo[i + 2 * k + 2]
        sumi = sumi - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] +last
        temp.append(sumi)
    # print(temp)
    print(max(temp))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]

if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
else:
    sumi = 0
    sumi = sum(memo[: k * 2 + b + 1])
    temp = [sumi]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(0, len(memo) - 2 * k  - 1, 2):
        sumi = sumi - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
        temp.append(sumi)
    # print(temp)
    print(max(temp))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]
sum = 0
if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
else:
    sum = sum(memo[: k * 2 + b + 1])
    temp = [sum]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(0, len(memo) - 2 * k  - 1, 2):
        sum = sum - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
        temp.append(sum)
    # print(temp)
    print(max(temp))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]
sum = 0
if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
    else:
    sum = sum(memo[: k * 2 + b + 1])
    temp = [sum]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(0, len(memo) - 2 * k  - 1, 2):
        sum = sum - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
        temp.append(sum)
    # print(temp)
    print(max(temp))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]

sum = sum(memo[: k * 2 + b + 1])
temp = [sum]
memo.append(0)
# print(memo)
# print(len(memo))
for i in range(0, len(memo) - 2 * k  - 1, 2):
    sum = sum - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
    temp.append(sum)
# print(temp)
print(max(temp))