Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
ans//=2
was=set()
for i in range(N):
    if ans<0:break
    for j in range(i+1,N):
        if ans<0:break
        for k in range(j+1,N):
            a,b,c=dist[i][j],dist[i][k],dist[j][k]
            if b<a and c<a:
                M=a
                S=b+c
                check=(i,j)
            elif a<b and c<b:
                M=b
                S=a+c
                check=(i,k)
            elif a<c and b<c:
                M=c
                S=a+b
                check=(j,k)
            else:continue
            if S==M:
                if check in was:continue
                ans-=M
                was|={check,}
            elif S<M:
                ans=-1*float('inf')
                break
if ans<0:ans=-1
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
for i in range(N):
    dist[i][i]=float('inf')
ans//=2
def main(ans):
    for i in range(N):
        for j in range(i+1,N):
            Min=min(map(sum,zip(dist[i],dist[j])))
            if Min<dist[i][j]:
                return -1
            if Min==dist[i][j]:
                ans-=dist[i][j] 
    return ans
print(main(ans))

Buggy python submission (verdict: Runtime Error):
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
ans//=2
for i in range(N):
    if ans<0:break
    for j in range(i+1,N):
        if ans<0:break
        for k in range(j+1,N):
            S=sum([dist[i][j],dist[i][k],dist[j][k]])/2
            M=max([dist[i][j],dist[i][k],dist[j][k]])
            if S==M:ans-=max(a,b,c)
            elif S<M:
                ans=-1*float('inf')
                break
if ans<0:ans=-1
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
for i in range(N):
    dist[i][i]=float('inf')
ans//=2
def main(ans):
    for i in range(N):
        for j in range(i+1,N):
            Min=min(map(sum,zip(dist[i],dist[j])))
            if Min<dist[i][j]:
                return -1
            if Min==dist[i][j]:
                ans-=dist[i][j] 
    return ans
print(main(ans))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
ans//=2
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            a,b,c=dist[i][j],dist[i][k],dist[j][k]
            if sum([a,b,c])/2==max(a,b,c):
                ans-=max(a,b,c)
            elif sum([a,b,c])/2<max(a,b,c):
                ans=-1*float('inf')
if ans<0:ans=-1
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
for i in range(N):
    dist[i][i]=float('inf')
ans//=2
def main(ans):
    for i in range(N):
        for j in range(i+1,N):
            Min=min(map(sum,zip(dist[i],dist[j])))
            if Min<dist[i][j]:
                return -1
            if Min==dist[i][j]:
                ans-=dist[i][j] 
    return ans
print(main(ans))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
ans//=2
for i in range(N):
    for j in range(i+1,N):
        for k in range(N):
            if k==i or k==j:continue
            if dist[i][k]+dist[j][k]==dist[i][j]:
                ans-=dist[i][j]
            elif dist[i][k]+dist[j][k]<dist[i][j]:
                ans=-1*float('inf')
if ans<0:ans=-1
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
N=int(input())
dist=[list(map(int,input().split())) for i in range(N)]
ans=sum([sum(dist[i]) for i in range(N)])
for i in range(N):
    dist[i][i]=float('inf')
ans//=2
def main(ans):
    for i in range(N):
        for j in range(i+1,N):
            Min=min(map(sum,zip(dist[i],dist[j])))
            if Min<dist[i][j]:
                return -1
            if Min==dist[i][j]:
                ans-=dist[i][j] 
    return ans
print(main(ans))