Problem p03074 (python)

Fixed submission:
import sys

n,k = map(int,input().split())
s = input()


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    print(len(s))
    sys.exit()    

if data[0][0] == '0':
    data.insert(0, (1,0))

if data[-1][0] == '0':
    data.append((1,0))

anstmp = 0
for i in range(2*k + 1):
    anstmp += data[i][1]

ans = max(ans, anstmp)

for i in range(zeros - k):
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i + 1][1]
    anstmp += data[2*k + 2*i + 2][1]

    ans = max(ans, anstmp)


print(ans)

Buggy submission (verdict: Runtime Error):
import sys

n,k = map(int,input().split())
s = input()


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    ans = len(s)

if data[0][0] == '0':
    data.insert(0, (1,0))

if data[-1][0] == '0':
    data.append((1,0))

anstmp = 0
for i in range(2*k + 1):
    anstmp += data[i][1]

ans = max(ans, anstmp)

for i in range(zeros - k):
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i + 1][1]
    anstmp += data[2*k + 2*i + 2][1]

    ans = max(ans, anstmp)


print(ans)

Buggy submission (verdict: Runtime Error):
import sys

n,k = map(int,input().split())
s = input()


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    ans = len(s)

elif s[0] == '0' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]
    # print('次',anstmp)
    ans = max(ans, anstmp)

    for i in range(1,zeros - k - 1):
        anstmp -= data[2*i + 1][1]
        anstmp -= data[2*i + 2][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i + 1][1]
    anstmp -= data[2*i + 2][1]
    anstmp += data[2*k + 2*i][1]

    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]
    
        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
 
    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]

    ans = max(ans, anstmp)
    
    for i in range(zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]

        ans = max(ans, anstmp)

if s[0] == '0' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]

    ans = max(ans, anstmp)
    
    for i in range(1, zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        ans = max(ans, anstmp)

print(ans)

Buggy submission (verdict: Runtime Error):
import sys

n,k = map(int,input().split())
s = input()


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    ans = len(s)

elif s[0] == '0' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp += data[2*k + 2*i][1]

    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]
    
        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
 
    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]

    ans = max(ans, anstmp)
    
    for i in range(zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]

        ans = max(ans, anstmp)

if s[0] == '0' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]

    ans = max(ans, anstmp)
    
    for i in range(1, zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        ans = max(ans, anstmp)

print(ans)