Buggy python submission (verdict: Time Limit Exceeded):
#16:15
n = int(input())
xyi = []
for i in range(n):
  xyi.append(list(map(int,input().split())) + [i])
xyi.sort()
road = [[] for _ in range(n)]
for j in range(n-1):
  z1 = xyi[j][2]
  z2 = xyi[j+1][2]
  d  = xyi[j+1][0] - xyi[j][0]
  road[z1].append([d,z2])
  road[z2].append([d,z1])
xyi.sort(key = lambda x: x[1])
for j in range(n-1):
  z1 = xyi[j][2]
  z2 = xyi[j+1][2]
  d  = xyi[j+1][1] - xyi[j][1]
  road[z1].append([d,z2])
  road[z2].append([d,z1])
pre = [i for i in range(n)]
ans = 0
seen = [0 for _ in range(n)]
seen[0] = 1
now = road[0]
import heapq
heapq.heapify(now)
ans = 0
while now:
  d,z = heapq.heappop(now)
  if seen[z] == 1:
    continue
  seen[z] = 1
  ans += d
  for x in road[z]:
    if seen[x[1]] == 0:
      heapq.heappush(now,x)
#print(seen)
#print(road)
print(ans)

Fixed submission:
#16:15
n = int(input())
xyi = []
for i in range(n):
  xyi.append(list(map(int,input().split())) + [i])
xyi.sort()
road = []
for j in range(n-1):
  road.append([xyi[j+1][0]-xyi[j][0],xyi[j][2],xyi[j+1][2]])
xyi.sort(key = lambda x: x[1])
for j in range(n-1):
  road.append([xyi[j+1][1]-xyi[j][1],xyi[j][2],xyi[j+1][2]])
road.sort()
pre = [i for i in range(n)]
ans = 0
for k in range(2*n-2):
  d,I,J = road[k]
  ii = []
  jj = []
  while pre[I] != I:
    ii.append(I)
    I = pre[I]
  for Is in ii:
    pre[Is] = I
  while pre[J] != J:
    jj.append(J)
    J = pre[J]
  for Js in jj:
    pre[Js] = J
  if I != J:
    pre[J] = I
    ans += d
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#16:15
n = int(input())
xyi = []
for i in range(n):
  xyi.append(list(map(int,input().split())) + [i])
xyi.sort()
road = []
for j in range(n-1):
  road.append([xyi[j+1][0]-xyi[j][0],xyi[j][2],xyi[j+1][2]])
xyi.sort(key = lambda x: x[1])
for j in range(n-1):
  road.append([xyi[j+1][1]-xyi[j][1],xyi[j][2],xyi[j+1][2]])
road.sort()
pre = [i for i in range(n)]
ans = 0
for k in range(2*n-2):
  d,I,J = road[k]
  ii = []
  ic = 0
  jj = []
  jc = 0
  while pre[I] != I:
    ii.append(I)
    ic += 1
    I = pre[I]
  while pre[J] != J:
    jj.append(J)
    jc += 1
    J = pre[J]
  if I != J:
    ans += d
    if ic >= jc:
      for Js in jj:
        pre[Js] = I
      pre[J] = I
    else:
      for Is in ii:
        pre[Is] = J
      pre[I] = J
print(ans)

Fixed submission:
#16:15
n = int(input())
xyi = []
for i in range(n):
  xyi.append(list(map(int,input().split())) + [i])
xyi.sort()
road = []
for j in range(n-1):
  road.append([xyi[j+1][0]-xyi[j][0],xyi[j][2],xyi[j+1][2]])
xyi.sort(key = lambda x: x[1])
for j in range(n-1):
  road.append([xyi[j+1][1]-xyi[j][1],xyi[j][2],xyi[j+1][2]])
road.sort()
pre = [i for i in range(n)]
ans = 0
for k in range(2*n-2):
  d,I,J = road[k]
  ii = []
  jj = []
  while pre[I] != I:
    ii.append(I)
    I = pre[I]
  for Is in ii:
    pre[Is] = I
  while pre[J] != J:
    jj.append(J)
    J = pre[J]
  for Js in jj:
    pre[Js] = J
  if I != J:
    pre[J] = I
    ans += d
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#16:15
n = int(input())
xyi = []
for i in range(n):
  xyi.append(list(map(int,input().split())) + [i])
xyi.sort()
road = []
for j in range(n-1):
  road.append([xyi[j+1][0]-xyi[j][0],xyi[j][2],xyi[j+1][2]])
xyi.sort(key = lambda x: x[1])
for j in range(n-1):
  road.append([xyi[j+1][1]-xyi[j][1],xyi[j][2],xyi[j+1][2]])
road.sort()
pre = [i for i in range(n)]
ans = 0
for k in range(2*n-2):
  d,I,J = road[k]
  while pre[I] != I:
    I = pre[I]
  while pre[J] != J:
    J = pre[J]
  if I != J:
    pre[J] = I
    ans += d
print(ans)

Fixed submission:
#16:15
n = int(input())
xyi = []
for i in range(n):
  xyi.append(list(map(int,input().split())) + [i])
xyi.sort()
road = []
for j in range(n-1):
  road.append([xyi[j+1][0]-xyi[j][0],xyi[j][2],xyi[j+1][2]])
xyi.sort(key = lambda x: x[1])
for j in range(n-1):
  road.append([xyi[j+1][1]-xyi[j][1],xyi[j][2],xyi[j+1][2]])
road.sort()
pre = [i for i in range(n)]
ans = 0
for k in range(2*n-2):
  d,I,J = road[k]
  ii = []
  jj = []
  while pre[I] != I:
    ii.append(I)
    I = pre[I]
  for Is in ii:
    pre[Is] = I
  while pre[J] != J:
    jj.append(J)
    J = pre[J]
  for Js in jj:
    pre[Js] = J
  if I != J:
    pre[J] = I
    ans += d
print(ans)