Problem p03074 (python)

Fixed submission:
# coding:utf-8
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():
    N,K = LMIIS()
    S = input().strip()
    nums = []
    count = 0
    conti = '0'
    for s in S:
        if s == conti:
            count += 1
        else:
            conti = s
            nums.append(count)
            count = 1
    nums.append(count)
    nums.append(0)
    

    i = 0
    tmpsum = sum(nums[:2*K])
    max_c = tmpsum
    for i in range(2*K+1,len(nums),2):
        tmpsum = tmpsum - nums[i- 2*K -1] - nums[i- 2*K] + nums[i-1] + nums[i]
        max_c = max(max_c,tmpsum)
    
    tmpsum = sum(nums[1:2*K+2])
    max_c = max(max_c,tmpsum)
    for i in range(2*K+3,len(nums),2):
        tmpsum = tmpsum - nums[i- 2*K -2] - nums[i-2*K -1] + nums[i-1] + nums[i]
        max_c = max(max_c,tmpsum)

    print(max_c)
        


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# coding:utf-8
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():
    N,K = LMIIS()
    S = input()

    leftc1 = 0
    rightc1 = 0
    tmpnum0 = 0
    cnum1s = []


    max_left = 0
    max_0 = 0
    max_right = 0
    max_c = 0
    max_c_idx = 0
    for i in range(N):

        if S[i] == '1':

            rightc1 += 1


        else:
            if rightc1 != 0:
                cnum1s.append([leftc1,tmpnum0,rightc1])

                if max_c < leftc1+tmpnum0+rightc1:
                    max_c = leftc1+tmpnum0+rightc1
                    max_c_idx = len(cnum1s) - 1
                    max_right = rightc1
                    max_0 = tmpnum0
                    max_left = leftc1

                leftc1 = rightc1
                rightc1 = 0
                tmpnum0 = 1
            else:
                tmpnum0 += 1
    cnum1s.append([leftc1 , tmpnum0 , rightc1])
    if max_c < leftc1 + tmpnum0 + rightc1:
        max_c = leftc1 + tmpnum0 + rightc1
        max_c_idx = len(cnum1s) - 1
        max_right = rightc1
        max_0 = tmpnum0
        max_left = leftc1
    if S[0] == '1':
        cnum1s.pop(0)
        max_c_idx -= 1

    ans = 0

    count = 0
    while count < K and len(cnum1s) != 0:
        dbg(cnum1s)
        count += 1
        sumleft = 0
        sumright = 0

        dbg(cnum1s, max_c_idx,max_left,max_0,max_right)
        if max_c_idx >= 1:

            cnum1s[max_c_idx - 1][2] += max_0 + max_right
            sumleft = sum(cnum1s[max_c_idx - 1])
        if max_c_idx < len(cnum1s) - 1:
            cnum1s[max_c_idx + 1][0] += max_0 + max_left
            sumright = sum(cnum1s[max_c_idx + 1])




        if sumleft > sumright:

            max_c = sumleft
            max_left = cnum1s[max_c_idx - 1][0]
            max_0 = cnum1s[max_c_idx - 1][1]
            max_right = cnum1s[max_c_idx - 1][2]
            cnum1s.pop(max_c_idx)
            max_c_idx -= 1
        elif sumright > sumleft:
            max_c = sumright
            max_left = cnum1s[max_c_idx +1][0]
            max_0 = cnum1s[max_c_idx + 1][1]
            max_right = cnum1s[max_c_idx + 1][2]
            cnum1s.pop(max_c_idx)
        else:
            break



    dbg(cnum1s)



    # for i in range(K):
    #     dbg(cnum1s,ans)
    #     max_left = 0
    #     max_0 = 0
    #     max_right = 0
    #     max_c = 0
    #     max_c_idx = 0
    #     if len(cnum1s) <= 1:
    #         break
    #     for j,(leftc1,tmpnum0,rightc1) in enumerate(cnum1s):
    #         if max_c < leftc1+tmpnum0+rightc1:
    #             max_c = leftc1+tmpnum0+rightc1
    #             max_left = leftc1
    #             max_0 = tmpnum0
    #             max_right = rightc1
    #             max_c_idx = j
    #
    #     if max_c_idx >= 1:
    #
    #         cnum1s[max_c_idx - 1][2] += max_0 + max_right
    #     if max_c_idx < len(cnum1s) -1:
    #         cnum1s[max_c_idx + 1][0] += max_0 + max_left
    #     cnum1s.pop(max_c_idx)
    #     ans += max_c

    print(max(cnum1s[max_c_idx][0],cnum1s[max_c_idx][2]))





    pass
if __name__ == '__main__':
    main()