Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop,heapify
INF=10**18

def find(x):
    if(par[x]==x):
        return x
    else:
        par[x]=find(par[x])
        return par[x]
def union(a,b):
    a=find(a)
    b=find(b)
    if(a==b):
        return 0
    else:
        if rank[a]>rank[b]:
            par[b]=a
            siz[a]+=siz[b]
        else:
            par[a]=b
            siz[b]+=siz[a]
            if rank[a]==rank[b]:
                rank[b]+=1
def size(a):
    return siz[find(a)]
def same(a,b):
    return find(a)==find(b)

def dijkstra(G,s,g,n):
    que=[(0,s)]
    dist=[INF]*n
    dist[s]=0
    while que:
        mincost,u=heappop(que)
        if(mincost>dist[u]):
            continue
        if(u==g):
            return dist
        if(G[u]):
            for c,v in G[u]:
                if(dist[u]+c<dist[v]):
                    dist[v]=dist[u]+c
                    heappush(que,(dist[v],v))
    return dist

N=int(input())
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]

A=[list(map(int,input().split())) for i in range(N)]

G=[set() for i in range(N)]
Edges=[]
for i in range(N):
    for j in range(N):
        if(i==j):
            continue
        Edges.append([A[i][j],i,j])
Edges.sort(key=lambda x:x[0])

res=0
for i in range(len(Edges)):
    cost,fr,to=Edges[i]
    if not same(fr,to):
        union(fr,to)
        G[fr].add((cost,to))
        G[to].add((cost,fr))
        res+=cost
    else:
        di=dijkstra(G,fr,to,N)
        if di[to]<A[fr][to]:
            print(-1)
            exit()
        if di[to]>cost:
            G[fr].add((cost,to))
            G[to].add((cost,fr))
            res+=cost
print(res)

Fixed submission:
N=int(input())
A=[list(map(int,input().split())) for i in range(N)]
res=0
flag=[[False]*N for i in range(N)]
for k in range(N):
    for i in range(N):
        for j in range(N):
            if A[i][j]>A[i][k]+A[k][j]:
                print(-1)
                exit()
            elif k!=i and k!=j and A[i][j]==A[i][k]+A[k][j] and not flag[i][j] and not flag[j][i]:
                flag[i][j]=True
                flag[j][i]=True
                res-=A[i][j]
ans=sum(sum(A[i]) for i in range(N))//2+res

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop,heapify
INF=10**18

def find(x):
    if(par[x]==x):
        return x
    else:
        par[x]=find(par[x])
        return par[x]
def union(a,b):
    a=find(a)
    b=find(b)
    if(a==b):
        return 0
    else:
        if rank[a]>rank[b]:
            par[b]=a
            siz[a]+=siz[b]
        else:
            par[a]=b
            siz[b]+=siz[a]
            if rank[a]==rank[b]:
                rank[b]+=1
def size(a):
    return siz[find(a)]
def same(a,b):
    return find(a)==find(b)

def dijkstra(G,s,g,n):
    que=[(0,s)]
    dist=[INF]*n
    dist[s]=0
    while que:
        mincost,u=heappop(que)
        if(mincost>dist[u]):
            continue
        if(u==g):
            return dist
        if(G[u]):
            for c,v in G[u]:
                if(dist[u]+c<dist[v]):
                    dist[v]=dist[u]+c
                    heappush(que,(dist[v],v))
    return dist

N=int(input())
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]

A=[list(map(int,input().split())) for i in range(N)]

G=[[] for i in range(N)]
Edges=[]
for i in range(N):
    for j in range(N):
        if(i==j):
            continue
        Edges.append([A[i][j],i,j])
heapify(Edges)

res=0
while Edges:
    cost,fr,to=heappop(Edges)
    if not same(fr,to):
        union(fr,to)
        G[fr].append((cost,to))
        G[to].append((cost,fr))
        res+=cost
    else:
        di=dijkstra(G,fr,to,N)
        if di[to]<A[fr][to]:
            print(-1)
            exit()
        if di[to]>cost:
            G[fr].append((cost,to))
            G[to].append((cost,fr))
            res+=cost
print(res)

Fixed submission:
N=int(input())
A=[list(map(int,input().split())) for i in range(N)]
res=0
flag=[[False]*N for i in range(N)]
for k in range(N):
    for i in range(N):
        for j in range(N):
            if A[i][j]>A[i][k]+A[k][j]:
                print(-1)
                exit()
            elif k!=i and k!=j and A[i][j]==A[i][k]+A[k][j] and not flag[i][j] and not flag[j][i]:
                flag[i][j]=True
                flag[j][i]=True
                res-=A[i][j]
ans=sum(sum(A[i]) for i in range(N))//2+res

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop,heapify
INF=10**18

def find(x):
    if(par[x]==x):
        return x
    else:
        par[x]=find(par[x])
        return par[x]
def union(a,b):
    a=find(a)
    b=find(b)
    if(a==b):
        return 0
    else:
        if rank[a]>rank[b]:
            par[b]=a
            siz[a]+=siz[b]
        else:
            par[a]=b
            siz[b]+=siz[a]
            if rank[a]==rank[b]:
                rank[b]+=1
def size(a):
    return siz[find(a)]
def same(a,b):
    return find(a)==find(b)

def dijkstra(G,s,g,n):
    que=[(0,s)]
    dist=[INF]*n
    dist[s]=0
    while que:
        mincost,u=heappop(que)
        if(mincost>dist[u]):
            continue
        if(u==g):
            return dist
        if(G[u]):
            for c,v in G[u]:
                if(dist[u]+c<dist[v]):
                    dist[v]=dist[u]+c
                    heappush(que,(dist[v],v))
    return dist

N=int(input())
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]

A=[list(map(int,input().split())) for i in range(N)]

for k in range(N):
    for i in range(N):
        for j in range(N):
            if A[i][j]>A[i][k]+A[k][j]:
                print(-1)
                exit()

G=[[] for i in range(N)]
Edges=[]
for i in range(N):
    for j in range(N):
        if(i==j):
            continue
        Edges.append([A[i][j],i,j])
heapify(Edges)

res=0
while Edges:
    cost,fr,to=heappop(Edges)
    if not same(fr,to):
        union(fr,to)
        G[fr].append((cost,to))
        G[to].append((cost,fr))
        res+=cost
    else:
        di=dijkstra(G,fr,to,N)
        if di[to]>cost:
            G[fr].append((cost,to))
            G[to].append((cost,fr))
            res+=cost
print(res)

Fixed submission:
N=int(input())
A=[list(map(int,input().split())) for i in range(N)]
res=0
flag=[[False]*N for i in range(N)]
for k in range(N):
    for i in range(N):
        for j in range(N):
            if A[i][j]>A[i][k]+A[k][j]:
                print(-1)
                exit()
            elif k!=i and k!=j and A[i][j]==A[i][k]+A[k][j] and not flag[i][j] and not flag[j][i]:
                flag[i][j]=True
                flag[j][i]=True
                res-=A[i][j]
ans=sum(sum(A[i]) for i in range(N))//2+res

print(ans)