Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
input = sys.stdin.readline

N = int(input())
cityx = []
cityy = []
for i in range(N):
    x,y = map(int,input().split())
    cityx.append([x,i])
    cityy.append([y,i])
cityx.sort()
cityy.sort()

side = [[]for i in range(N)]
for i in range(N-1):
    a,x = cityx[i]
    b,y = cityx[i+1]
    side[x].append([b-a,y])
    side[y].append([b-a,x])
for i in range(N-1):
    a,x = cityy[i]
    b,y = cityy[i+1]
    side[x].append([b-a,y])
    side[y].append([b-a,x])

f = [1] + [0] * (N-1)
queue = side[0][:]
ans = 0
heapq.heapify(queue)
while queue:
    cost, q = heapq.heappop(queue)
    if f[q] == 0:
        ans += cost
        f[q] = 1
        for a,b in side[q]:
            heapq.heappush(queue,[a,b])

print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
cityx = [[0,i]for i in range(N)]
cityy = [[0,i]for i in range(N)]
for i in range(N):
    x,y = map(int,input().split())
    cityx[i][0] = x
    cityy[i][0] = y

cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
per = [i for i in range(N)]
rank = [0]*N
def find(x):
    if per[x] == x:
        return x
    else:
        per[x] = find(per[x])
        return per[x]

ans = 0

for cost,x,y in side:
    x = find(x)
    y = find(y)
    if x != y:
        ans += cost
        if rank[x] < rank[y]:
            per[x] = y
        else:
            per[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
class Unionfind:
    def __init__(self,n):
        self.per = [i for i in range(n)]

    def find(self, x):
        if self.per[x] == x:
            return x
        else:
            self.per[x] = self.find(self.per[x])
            return self.per[x]

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        self.per[x] = y
        
            
N = int(input())
cityx = []
cityy = []
for i in range(N):
    x,y = map(int,input().split())
    cityx.append([x,i])
    cityy.append([y,i])
cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
uf = Unionfind(N)
for cost, x, y in side:
    if uf.same_check(x,y):
        continue
    ans += cost
    uf.union(x,y)

print(ans)



Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
cityx = [[0,i]for i in range(N)]
cityy = [[0,i]for i in range(N)]
for i in range(N):
    x,y = map(int,input().split())
    cityx[i][0] = x
    cityy[i][0] = y

cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
per = [i for i in range(N)]
rank = [0]*N
def find(x):
    if per[x] == x:
        return x
    else:
        per[x] = find(per[x])
        return per[x]

ans = 0

for cost,x,y in side:
    x = find(x)
    y = find(y)
    if x != y:
        ans += cost
        if rank[x] < rank[y]:
            per[x] = y
        else:
            per[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
class Unionfind:
    def __init__(self,n):
        self.per = [i for i in range(n)]
        self.rank = [0]*n

    def find(self, x):
        if self.per[x] == x:
            return x
        else:
            self.per[x] = self.find(self.per[x])
            return self.per[x]

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.per[x] = y
        else:
            self.per[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

N = int(input())
cityx = []
cityy = []
for i in range(N):
    x,y = map(int,input().split())
    cityx.append([x,i])
    cityy.append([y,i])
cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
uf = Unionfind(N)
for cost, x, y in side:
    if uf.same_check(x,y):
        continue
    ans += cost
    uf.union(x,y)

print(ans)



Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
cityx = [[0,i]for i in range(N)]
cityy = [[0,i]for i in range(N)]
for i in range(N):
    x,y = map(int,input().split())
    cityx[i][0] = x
    cityy[i][0] = y

cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
per = [i for i in range(N)]
rank = [0]*N
def find(x):
    if per[x] == x:
        return x
    else:
        per[x] = find(per[x])
        return per[x]

ans = 0

for cost,x,y in side:
    x = find(x)
    y = find(y)
    if x != y:
        ans += cost
        if rank[x] < rank[y]:
            per[x] = y
        else:
            per[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
cityx = []
cityy = []
for i in range(N):
    x,y = map(int,input().split())
    cityx.append([x,i])
    cityy.append([y,i])
side = []
cityx.sort()
cityy.sort()
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0],cityx[i][1],cityx[i+1][1]])
    side.append([cityy[i+1][0]-cityy[i][0],cityy[i][1],cityy[i+1][1]])

side.sort()
node = [[i,[i]]for i in range(N)]
ans = 0
for cost,a,b in side:
    if node[a][0] != node[b][0]:
        ans += cost
        c,d = node[node[a][0]],node[node[b][0]]
        if len(c[1]) < len(d[1]):
            for i in c[1]:
                node[i][0] = d[0]
            d[1] = d[1] + c[1]
        else:
            for i in d[1]:
                node[i][0] = c[0]
            c[1] = d[1] + c[1]
print(ans)
            





Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
cityx = [[0,i]for i in range(N)]
cityy = [[0,i]for i in range(N)]
for i in range(N):
    x,y = map(int,input().split())
    cityx[i][0] = x
    cityy[i][0] = y

cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
per = [i for i in range(N)]
rank = [0]*N
def find(x):
    if per[x] == x:
        return x
    else:
        per[x] = find(per[x])
        return per[x]

ans = 0

for cost,x,y in side:
    x = find(x)
    y = find(y)
    if x != y:
        ans += cost
        if rank[x] < rank[y]:
            per[x] = y
        else:
            per[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
city = []
for i in range(N):
    x,y = map(int,input().split())
    city.append([x,y,i])
side = []
city.sort(key=lambda x:x[0])
for i in range(N-1):
    side.append([city[i+1][0] - city[i][0],city[i][2] ,city[i+1][2]])

city.sort(key=lambda x:x[1])
for i in range(N-1):
    side.append([city[i+1][1] - city[i][1],city[i][2] ,city[i+1][2]])
side.sort()
node = [[i,[i]]for i in range(N)]
ans = 0
for cost,a,b in side:
    if node[a][0] != node[b][0]:
        ans += cost
        c,d = node[node[a][0]],node[node[b][0]]
        if len(c[1]) < len(d[1]):
            for i in c[1]:
                node[i][0] = d[0]
            d[1] = d[1] + c[1]
        else:
            for i in d[1]:
                node[i][0] = c[0]
            c[1] = d[1] + c[1]
print(ans)
            





Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
cityx = [[0,i]for i in range(N)]
cityy = [[0,i]for i in range(N)]
for i in range(N):
    x,y = map(int,input().split())
    cityx[i][0] = x
    cityy[i][0] = y

cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
per = [i for i in range(N)]
rank = [0]*N
def find(x):
    if per[x] == x:
        return x
    else:
        per[x] = find(per[x])
        return per[x]

ans = 0

for cost,x,y in side:
    x = find(x)
    y = find(y)
    if x != y:
        ans += cost
        if rank[x] < rank[y]:
            per[x] = y
        else:
            per[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
city = []
for i in range(N):
    x,y = map(int,input().split())
    city.append([x,y,i])
side = []
city.sort(key=lambda x:x[0])
for i in range(N-1):
    side.append([city[i+1][0] - city[i][0],city[i][2] ,city[i+1][2]])

city.sort(key=lambda x:x[1])
for i in range(N-1):
    side.append([city[i+1][1] - city[i][1],city[i][2] ,city[i+1][2]])
side.sort()
node = [[i,[i]]for i in range(N)]
ans = 0
for cost,a,b in side:
    if node[a][0] != node[b][0]:
        ans += cost
        c,d = node[node[a][0]],node[node[b][0]]
        if len(c[1]) < len(d[1]):
            for i in c[1]:
                node[i][0] = d[0]
            d[1] = d[1] + c[1]
        else:
            for i in d[1]:
                node[i][0] = c[0]
            c[1] = d[1] + c[1]
print(ans)
            





Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
cityx = [[0,i]for i in range(N)]
cityy = [[0,i]for i in range(N)]
for i in range(N):
    x,y = map(int,input().split())
    cityx[i][0] = x
    cityy[i][0] = y

cityx.sort()
cityy.sort()
side = []
for i in range(N-1):
    side.append([cityx[i+1][0]-cityx[i][0], cityx[i+1][1], cityx[i][1]])
    side.append([cityy[i+1][0]-cityy[i][0], cityy[i+1][1], cityy[i][1]])
side.sort()
ans = 0
per = [i for i in range(N)]
rank = [0]*N
def find(x):
    if per[x] == x:
        return x
    else:
        per[x] = find(per[x])
        return per[x]

ans = 0

for cost,x,y in side:
    x = find(x)
    y = find(y)
    if x != y:
        ans += cost
        if rank[x] < rank[y]:
            per[x] = y
        else:
            per[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

print(ans)