Buggy python submission (verdict: Time Limit Exceeded):
def solve(V, G):
    for i in range(V):
        G[i][i] = float("inf")
    for k in range(V):
        for i in range(V):
            for j in range(V):
                if G[i][k] + G[k][j] < G[i][j] < float("inf"):
                    return False, G
                elif G[i][j] == G[i][k] + G[k][j]:
                    G[i][j] = float("inf")
    return True, G


def dist(V, G):
    ans = 0
    for i in range(V):
        for j in range(i, V):
            if G[i][j] < float("inf"):
                ans += G[i][j]
    return ans


def main():
    V = int(input())
    G = [[int(i) for i in input().split()] for i in range(V)]
    flag, G = solve(V, G)
    if flag:
        print(dist(V, G))
    else:
        print(-1)


if __name__ == "__main__":
    main()

Fixed submission:
def solve(V, G):
    ans = 0
    for i in range(V - 1):
        for j in range(i + 1, V):
            direct = G[i][j]
            for k in range(V):
                if k == i or k == j:
                    continue
                ind = G[i][k] + G[k][j]
                if direct > ind:
                    return -1
                elif direct == ind:
                    break
            else:
                ans += direct
    return ans


def main():
    V = int(input())
    G = [[int(i) for i in input().split()] for i in range(V)]
    print(solve(V, G))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    V = int(input())
    flag = True
    ans = 0

    G = [[int(i) for i in input().split()] for i in range(V)]
    for i in range(V):
        G[i][i] = float("inf")
    for k in range(V):
        for i in range(V):
            for j in range(V):
                if G[i][k] + G[k][j] < G[i][j] < float("inf"):
                    flag = False
                    break
                elif G[i][j] == G[i][k] + G[k][j]:
                    G[i][j] = float("inf")

    for i in range(V):
        for j in range(i, V):
            if G[i][j] < float("inf"):
                ans += G[i][j]

    if flag:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Fixed submission:
def solve(V, G):
    ans = 0
    for i in range(V - 1):
        for j in range(i + 1, V):
            direct = G[i][j]
            for k in range(V):
                if k == i or k == j:
                    continue
                ind = G[i][k] + G[k][j]
                if direct > ind:
                    return -1
                elif direct == ind:
                    break
            else:
                ans += direct
    return ans


def main():
    V = int(input())
    G = [[int(i) for i in input().split()] for i in range(V)]
    print(solve(V, G))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def solve(V, G):
    for i in range(V):
        G[i][i] = float("inf")
    for k in range(V):
        for i in range(V):
            for j in range(V):
                #print(G)
                if G[i][k] + G[k][j] < G[i][j] < float("inf"):
                    return False, G
                elif G[i][j] == G[i][k] + G[k][j]:
                    G[i][j] = float("inf")
    return True, G


def dist(G):
    ans = 0
    for i in range(N):
        for j in range(N):
            if G[i][j] < float("inf"):
                ans += G[i][j]
    return ans

N = int(input())
G = [[int(i) for i in input().split()] for i in range(N)]

flag, G = solve(N, G)

if flag:
    print(dist(G)//2)
else:
    print(-1)


Fixed submission:
def solve(V, G):
    ans = 0
    for i in range(V - 1):
        for j in range(i + 1, V):
            direct = G[i][j]
            for k in range(V):
                if k == i or k == j:
                    continue
                ind = G[i][k] + G[k][j]
                if direct > ind:
                    return -1
                elif direct == ind:
                    break
            else:
                ans += direct
    return ans


def main():
    V = int(input())
    G = [[int(i) for i in input().split()] for i in range(V)]
    print(solve(V, G))


if __name__ == "__main__":
    main()