Problem p03074 (python)

Fixed submission:
n,k=map(int,input().split())
s=input()
s=s+"w"
lists0=[]
lists1=[] 
count=0
for i in range(n):
    if s[i]!=s[i+1] and s[i]=="0":
        lists0.append(i-count)
        count=i
        
    elif s[i]!=s[i+1] and s[i]=="1":
        lists1.append(i-count)
        count=i

if s[0]=="1":
    lists1[0]+=1
elif s[0]=="0":
    lists0[0]+=1     
#下準備の完成
if s[0]=="1" and s[-2]=="1":
    #do nothing special
    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(big,max(ans,ans1))
            ans=ans1
        print(big)
       
        
elif s[0]=="1" and s[-2]=="0":
    # 最後にlists1の方に0を挿入する
    lists1.append(0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(max(ans,ans1),big)
            ans=ans1
        print(big)
    
elif s[0]=="0" and s[-2]=="1":
    #さいしょのlist1の方に0を挿入する
    lists1.insert(0,0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(big,max(ans,ans1))
            ans=ans1
        print(big)    
elif s[0]=="0" and s[-2]=="0":
    #最初と最後にlist1の方に0を挿入する
    lists1.insert(0,0)
    lists1.append(0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):            
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(big,max(ans,ans1))
            ans=ans1
        print(big)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
s=input()
s+="end"
if n>=2:
    uselist=[]
    for i in range(len(s)-1):
        if i==0 and s[i]=="0":
            uselist.append(0)
        if s[i]=="1" and s[i+1]=="0":
            uselist.append(i+1)
        if s[i+1]=="end":
            continue
    ans=0
    if uselist[0]>=1:
        uselist.insert(0,0)
        length=len(uselist)

        for i in range(length):
            if i+k+1<=length-1:
                ans=max(ans,uselist[i+k+1]-uselist[i]-1)

            else:
                ans=max(ans,n-uselist[i]-1)
    
                break
        print(ans)


    elif uselist[0]==0:
        uselist.insert(0,-1)

        length=len(uselist)
        for i in range(length):
            if i+k+1<=length-1:
                ans=max(ans,uselist[i+k+1]-uselist[i]-1)

            else:
                ans=max(ans,n-uselist[i]-1)

                break
        print(ans)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
s=input()
s+="end"

uselist=[]
for i in range(len(s)-1):
    if i==0 and s[i]=="0":
        uselist.append(0)
    if s[i]=="1" and s[i+1]=="0":
        uselist.append(i+1)
    if s[i+1]=="end":
        continue
ans=0
if uselist[0]>=1:
    uselist.insert(0,0)
    length=len(uselist)

    for i in range(length):
        if i+k+1<=length-1:
            ans=max(ans,uselist[i+k+1]-uselist[i]-1)

        else:
            ans=max(ans,n-uselist[i]-1)

            break
    print(ans)
    print(uselist)

elif uselist[0]==0:
    uselist.insert(0,-1)

    length=len(uselist)
    for i in range(length):
        if i+k+1<=length-1:
            ans=max(ans,uselist[i+k+1]-uselist[i]-1)

        else:
            ans=max(ans,n-uselist[i]-1)

            break
    print(ans)