Problem p03600 (python)

Fixed submission:
def main():
    N = int(input())
    l = []
    for i in range(N):
        a = list(map(int, input().split()))
        l.append(a)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            if i == j:
                continue
            for k in range(N):
                if k == i or k == j:
                    continue
                if l[i][j] == l[i][k] + l[k][j]:
                    break
                if l[i][j] > l[i][k] + l[k][j]:
                    return -1
            else:
                ans += l[i][j]
    return ans
print(main())

Buggy submission (verdict: Runtime Error):
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def size(self, x):
        return -self.parents[self.find(x)]
        
    def same(self, x, y):
        return self.find(x) == self.find(y)
    
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())
    
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
    
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    N = int(input())
    l = []
    d = {}
    u = UnionFind(N)
    for i in range(N):
        a = list(map(int, input().split()))
        for j in range(N):
            if i < j:
                l.append((a[j], i, j))
                d[(i, j)] = a[j]
                d[(j, i)] = a[j]
    l.sort()
    r = [[0]*N for _ in range(N)]
    ans = 0
    for k, i, j in l:
        if not u.same(i,j):
            r[i][j] = k
            r[j][i] = k
            ans += k
            for w in u.members(i):
                if w == i:
                    continue
                if r[i][w] + k < d[(w,j)]:
                    return -1
                else:
                    r[w][j] = r[i][w] + k
                    r[j][w] = r[i][w] + k
            for w in u.members(j):
                if w == j:
                    continue
                if r[j][w] + k < d[(w,i)]:
                    return -1
                else:
                    r[w][i] = r[j][w] + k
                    r[i][w] = r[j][w] + k
            u.union(i,j)
            continue
        if r[i][j] < k:
            return -1
        elif r[i][j] == k:
            continue
        else:
            r[i][j] = k
            r[j][i] = k
            ans += k
            for w in u.members(i):
                r[i][w] = r[w][i] = min(r[i][w], r[j][w] + k)
                if r[i][w] < d[(i,w)]:
                    return -1
                r[j][w] = r[w][j] = min(r[j][w], r[i][w] + k)
                if r[j][w] < d[(j,w)]:
                    return -1
    return ans
print(main())