Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)


Fixed submission:
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    x=t[x]
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Fixed submission:
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    x=t[x]
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Fixed submission:
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[[]for i in range(n)]
for i in range(n-1):
    p,q,r=a[i][0],a[i+1][0],a[i+1][1]-a[i][1]
    s[p].append([r,p,q])
    s[q].append([r,q,p])
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    p,q,r=a[i][0],a[i+1][0],a[i+1][2]-a[i][2]
    s[p].append([r,p,q])
    s[q].append([r,q,p])
t=[1]*n
t[0],q,r=0,[],1
e=0
for i in s[0]:
    heappush(q,i)
while q and r!=n:
    b,c,d=heappop(q)
    if t[d]==1:
        t[d]=0
        for i in s[d]:
            if t[i[2]]==1:
                heappush(q,i)
        e+=b
        r+=1
print(e)

Fixed submission:
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[[]for i in range(n)]
for i in range(n-1):
    p,q,r=a[i][0],a[i+1][0],a[i+1][1]-a[i][1]
    s[p].append([r,p,q])
    s[q].append([r,q,p])
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    p,q,r=a[i][0],a[i+1][0],a[i+1][2]-a[i][2]
    s[p].append([r,p,q])
    s[q].append([r,q,p])
t=[1]*n
t[0],q=0,[]
e=0
for i in s[0]:
    heappush(q,i)
while q and t!=[0]*n:
    b,c,d=heappop(q)
    if t[d] or t[c]:
        if t[d]==1:
            for i in s[d]:
                heappush(q,i)
            t[d]=0
        if t[c]==1:
            for i in s[c]:
                heappush(q,i)
            t[c]=0
        e+=b
print(e)

Fixed submission:
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[[]for i in range(n)]
for i in range(n-1):
    p,q,r=a[i][0],a[i+1][0],a[i+1][1]-a[i][1]
    s[p].append([r,p,q])
    s[q].append([r,q,p])
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    p,q,r=a[i][0],a[i+1][0],a[i+1][2]-a[i][2]
    s[p].append([r,p,q])
    s[q].append([r,q,p])
t=[1]*n
t[0],q=0,[]
e=0
for i in s[0]:
    heappush(q,i)
while q and t!=[0]*n:
    b,c,d=heappop(q)
    if t[d] or t[c]:
        if t[d]==1:
            for i in s[d]:
                heappush(q,i)
            t[d]=0
        if t[c]==1:
            for i in s[c]:
                heappush(q,i)
            t[c]=0
        e+=b
print(e)

Fixed submission:
n=int(input())
a=sorted([[i]+list(map(int,input().split()))for i in range(n)],key=lambda x:x[1])
s=[]
for i in range(n-1):
    s.append((a[i+1][1]-a[i][1],a[i][0],a[i+1][0]))
a=sorted(a,key=lambda x:x[2])
for i in range(n-1):
    s.append((a[i+1][2]-a[i][2],a[i][0],a[i+1][0]))
s.sort()
t=[i for i in range(n)]
def f(x):
    if x==t[x]:
        return x
    t[x]=f(t[x])
    return f(t[x])
c=0
for i,j,h in s:
    j,h=f(j),f(h)
    if h!=j:
        t[j]=h
        c+=i
print(c)