Buggy python submission (verdict: Time Limit Exceeded):
def main() :
    n = int(input())
    A = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for i in range(n) :
        for j in range(i+1, n) :
            cost = A[i][j]
            for k in range(n) :
                if i == k or j == k:
                    continue

                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    cost = 0
                elif d < A[i][j] :
                    print(-1)
                    exit()
            ans += cost
    print(ans)

main()

Fixed submission:
def main() :
    n = int(input())
    A = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for i in range(n) :
        for j in range(i+1, n) :
            cost = A[i][j]
            for k in range(n) :
                if i == k or j == k:
                    continue

                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    cost = 0
                elif d < A[i][j] :
                    print(-1)
                    exit()
            ans += cost
    print(ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main() :
    n = int(input())
    A = []
    for _ in range(n) :
        A.append(list(map(int, input().split())))

    ans = sum([sum(_A) for _A in A])
    for k in range(n) :
        for i in range(n) :
            for j in range(n) :
                if i == k :
                    break
                elif j == k :
                    continue
                
                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    ans -= d
                elif d < A[i][j] :
                    print(-1)
                    exit()

    print(ans//2)

main()

Fixed submission:
def main() :
    n = int(input())
    A = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for i in range(n) :
        for j in range(i+1, n) :
            cost = A[i][j]
            for k in range(n) :
                if i == k or j == k:
                    continue

                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    cost = 0
                elif d < A[i][j] :
                    print(-1)
                    exit()
            ans += cost
    print(ans)

main()

Buggy python submission (verdict: Runtime Error):
def main()
    n = int(input())
    A = []
    for _ in range(n) :
        A.append(list(map(int, input().split())))

    ans = sum([sum(_A) for _A in A])
    for k in range(n) :
        for i in range(n) :
            for j in range(n) :
                if i == k :
                    break
                elif j == k :
                    continue
                
                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    ans -= d
                elif d < A[i][j] :
                    print(-1)
                    exit()

    print(ans//2)

main()

Fixed submission:
def main() :
    n = int(input())
    A = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for i in range(n) :
        for j in range(i+1, n) :
            cost = A[i][j]
            for k in range(n) :
                if i == k or j == k:
                    continue

                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    cost = 0
                elif d < A[i][j] :
                    print(-1)
                    exit()
            ans += cost
    print(ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = []
for _ in range(n) :
    A.append(list(map(int, input().split())))

wf = [[A[i][j] for j in range(n)] for i in range(n)]
ans = sum([sum(_A) for _A in A])
for k in range(n) :
    for i in range(n) :
        for j in range(n) :
            if i == k :
                break
            elif j == k :
                continue
            
            d = wf[i][k] + wf[k][j]
            if d == wf[i][j] :
                ans -= d
            elif d < wf[i][j] :
                print(-1)
                exit()

print(ans//2)

Fixed submission:
def main() :
    n = int(input())
    A = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for i in range(n) :
        for j in range(i+1, n) :
            cost = A[i][j]
            for k in range(n) :
                if i == k or j == k:
                    continue

                d = A[i][k] + A[k][j]
                if d == A[i][j] :
                    cost = 0
                elif d < A[i][j] :
                    print(-1)
                    exit()
            ans += cost
    print(ans)

main()