Problem p03074 (python)

Fixed submission:
N,K=map(int,input().split())
s=list(input())

S=[]
F="2"
cnt=0
for i in range(N):
    if F=="0":
        if s[i]=="0":
            cnt-=1
        else:
            S.append(cnt)
            F="1"
            cnt=1
    elif F=="1":
        if s[i]=="1":
            cnt+=1
        else:
            S.append(cnt)
            F="0"
            cnt=-1
    else:
        if s[i]=="0":
            F="0"
            cnt=-1
        else:
            F="1"
            cnt=1
S.append(cnt)
if S[0]<0:
    S=[0]+S+[0]
else:
    S=S+[0]

if S[0]>=0:
    if len(S)<=(2*K+1):
        ans=0
        for i in range(len(S)):
            ans+=abs(S[i])
        print(ans)
        #exit()
    else:
        ans=0
        SUM=0
        for i in range(2*K+1):
            SUM+=abs(S[i])
            ans=SUM
        #print(ans)
        for i in range(1,len(S)):
            if (2*i+2*K)>(len(S)-1):
                #print("A")
                break
            else:
                #print("B")
                SUM=SUM+abs(S[2*i+2*K-1])+abs(S[2*i+2*K])-abs(S[2*i-1])-abs(S[2*i-2])
                if SUM>ans:
                    ans=SUM
        print(ans)
        #exit()
#print(S)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
S=input()

L=[]

P=S[0]
cnt=1
for i in range(1,N):
    if S[i]==P:
        cnt+=1
    else:
        if P=="1":
            L.append(cnt)
            cnt=1
            P="0"
        else:
            L.append(cnt*-1)
            cnt=1
            P="1"
if P=="1":
    L.append(cnt)
else:
    L.append(cnt*-1)
if len(L)==1:
    print(L)
    exit()
X=[0 for i in range(len(L))]
L=X+L+X
#print(L)
ANS=[]
NUM=0
for i in range(1,2*K-1):
    NUM+=abs(L[i])
ANS=[NUM]
#print(ANS)

SUM=[0]
for i in range(1,len(L)):
    SUM.append(SUM[i-1]+abs(L[i]))



ans=0
if len(SUM)-len(X)<2*K:
    print(SUM[len(SUM)-1])
else:    
    for i in range(len(SUM)-len(X)):
        if L[i]<0:
            if (SUM[2*K+1+i]-SUM[i])>ans:
                ans=(SUM[2*K+1+i]-SUM[i])
        else:
            if (SUM[2*K+i]-SUM[i])>ans:
                ans=(SUM[2*K+i]-SUM[i])
            #print(i)
    print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
S=input()

L=[]

P=S[0]
cnt=1
for i in range(1,N):
    if S[i]==P:
        cnt+=1
    else:
        if P=="1":
            L.append(cnt)
            cnt=1
            P="0"
        else:
            L.append(cnt*-1)
            cnt=1
            P="1"
if P=="1":
    L.append(cnt)
else:
    L.append(cnt*-1)
X=[0 for i in range(len(L))]
L=X+L+X
#print(L)
ANS=[]
NUM=0
for i in range(1,2*K-1):
    NUM+=abs(L[i])
ANS=[NUM]
#print(ANS)

SUM=[0]
for i in range(1,len(L)):
    SUM.append(SUM[i-1]+abs(L[i]))

ans=0
if len(SUM)-len(X)<2*K+1:
    print(SUM[len(SUM)-1])
else:    
    for i in range(len(SUM)-len(X)):
        if L[i]<0:
            if (SUM[2*K+1+i]-SUM[i])>ans:
                ans=(SUM[2*K+1+i]-SUM[i])
        else:
            if (SUM[2*K+i]-SUM[i])>ans:
                ans=(SUM[2*K+i]-SUM[i])
            #print(i)
    print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
S=input()

L=[]

P=S[0]
cnt=1
for i in range(1,N):
    if S[i]==P:
        cnt+=1
    else:
        if P=="1":
            L.append(cnt)
            cnt=1
            P="0"
        else:
            L.append(cnt*-1)
            cnt=1
            P="1"
if P=="1":
    L.append(cnt)
else:
    L.append(cnt*-1)
X=[0 for i in range(len(L))]
L=X+L+X
#print(L)
ANS=[]
NUM=0
for i in range(1,2*K-1):
    NUM+=abs(L[i])
ANS=[NUM]
#print(ANS)

SUM=[0]
for i in range(1,len(L)):
    SUM.append(SUM[i-1]+abs(L[i]))

ans=0
if len(SUM)-len(X)<2*K+1:
    print(SUM[len(SUM)-1])
else:    
    for i in range(len(SUM)-len(X)):
        if L[i]<0:
            if (SUM[2*K+1+i]-SUM[i])>ans:
                ans=(SUM[2*K+1+i]-SUM[i])
        else:
            if (SUM[2*K+i]-SUM[i])>ans:
                ans=(SUM[2*K+i]-SUM[i])
            #print(i)
    print(ans)