Problem p02761 (python)

Fixed submission:
#なんもわからんくなった
N,M=map(int,input().split())
s=[]
c=[]
ans=0
#入力
for i in range(M):
    x,y=map(int,input().split())
    s.append(x)
    c.append(y)

if M==0:
    if N==1:
        print(0)
        exit()
    elif N==2:
        print(10)
        exit()
    else:
        print(100)
        exit()
#同じ桁だけど数が違う場合
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]!=c[j]:
            print(-1)
            exit()

#1桁目が0なのにNが2桁以上の時
for i in range(M):
    if s[i]==1 and c[i]==0 and N>1:
        print(-1)
        exit()

#処理の重複を消してる
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]==c[j]:
            s[j]=0
            c[j]=0

#各桁ごとの最小値
if N==1:
    print(c[0])
    exit()
if N==2:
    ans=10
    for i in range(M):
        if s[i]==1:
            ans=0
    for i in range(M):
        if s[i]==1:
            ans+=10*c[i]
        if s[i]==2:
            ans+=c[i]
    print(ans)
    exit()
if N==3:
    ans=100
    for i in range(M):
        if s[i]==1:
            ans=0
    for i in range(M):
        if s[i]==1:
            ans+=100*c[i]
        if s[i]==2:
            ans+=10*c[i]
        if s[i]==3:
            ans+=c[i]
    print(ans)

Buggy submission (verdict: Runtime Error):
#なんもわからんくなった
N,M=map(int,input().split())
s=[]
c=[]
ans=0
#入力
for i in range(M):
    x,y=map(int,input().split())
    s.append(x)
    c.append(y)

#同じ桁だけど数が違う場合
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]!=c[j]:
            print(-1)
            exit()

#1桁目が0なのにNが2桁以上の時
for i in range(M):
    if s[i]==1 and c[i]==0 and N>1:
        print(-1)
        exit()

#処理の重複を消してる
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]==c[j]:
            s[j]=0
            c[j]=0

#各桁ごとの最小値
if N==1:
    print(c[0])
    exit()
if N==2:
    ans=10
    for i in range(M):
        if s[i]==1:
            ans=0
    for i in range(M):
        if s[i]==1:
            ans+=10*c[i]
        if s[i]==2:
            ans+=c[i]
    print(ans)
    exit()
if N==3:
    ans=100
    for i in range(M):
        if s[i]==1:
            ans=0
    for i in range(M):
        if s[i]==1:
            ans+=100*c[i]
        if s[i]==2:
            ans+=10*c[i]
        if s[i]==3:
            ans+=c[i]
    print(ans)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
s=[]
c=[]
ans=0
for i in range(M):
    x,y=map(int,input().split())
    s.append(x)
    c.append(y)
    
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]!=c[j]:
            print(-1)
            exit()

for i in range(M):
    if N<s[i]:
        print(-1)
        exit()

for i in range(M):
    if s[i]==1 and c[i]==0 and N>1:
        print(-1)
        exit()

for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]==c[j]:
            s[j]=0
            c[j]=0
if N==1:
    print(c[0])
    exit()
if N==2:
    ans=10
    for i in range(M):
        if s[i]==1:
            ans=0
    for i in range(M):
        if s[i]==1:
            ans+=10*c[i]
        if s[i]==2:
            ans+=c[i]
    print(ans)
    exit()
if N==3:
    ans=100
    for i in range(M):
        if s[i]==1:
            ans=0
    for i in range(M):
        if s[i]==1:
            ans+=100*c[i]
        if s[i]==2:
            ans+=10*c[i]
        if s[i]==3:
            ans+=c[i]
    print(ans)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
s=[]
c=[]
ans=0
for i in range(M):
    x,y=map(int,input().split())
    s.append(x)
    c.append(y)
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]!=c[j]:
            print(-1)
            exit()
for i in range(M):
    if N<s[i]:
        print(-1)
        exit()
for i in range(M):
    if s[i]==1 and c[i]==0 and N>1:
        print(-1)
        exit()
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]==c[j]:
            s[j]=0
            c[j]=0
if N==1:
    print(c[0])
if N==2:
    for i in range(M):
        if s[i]==1:
            ans+=10*c[i]
        if s[i]==2:
            ans+=c[i]
    print(ans)
    exit()
if N==3:
    for i in range(M):
        if s[i]==1:
            ans+=100*c[i]
        if s[i]==2:
            ans+=10*c[i]
        if s[i]==3:
            ans+=c[i]
    print(ans)