Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input())
root = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int,input().split())
    root[u-1].append([v-1,w%2])
    root[v-1].append([u-1,w%2])

ans = [-1 for i in range(N)]

def dfs(index):
    if ans[index]==-1:
        ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

for i in ans:
    print(i)

Fixed submission:
import sys
sys.setrecursionlimit(4100000)

N = int(input())
root = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int,input().split())
    root[u-1].append([v-1,w%2])
    root[v-1].append([u-1,w%2])

ans = [-1 for i in range(N)]

def dfs(index):
    if ans[index]==-1:
        ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

for i in ans:
    print(i)

Buggy python submission (verdict: Runtime Error):
N = int(input())
root = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int,input().split())
    root[u-1].append([v-1,w%2])
    root[v-1].append([u-1,w%2])

ans = [-1 for i in range(N)]

def dfs(index):
    if ans[index]==-1:
        ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

for i in ans:
    print(i)

Fixed submission:
import sys
sys.setrecursionlimit(4100000)

N = int(input())
root = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int,input().split())
    root[u-1].append([v-1,w%2])
    root[v-1].append([u-1,w%2])

ans = [-1 for i in range(N)]

def dfs(index):
    if ans[index]==-1:
        ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

for i in ans:
    print(i)

Buggy python submission (verdict: Runtime Error):
N = int(input())
root = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int,input().split())
    root[u-1].append([v-1,w%2])
    root[v-1].append([u-1,w%2])

ans = [-1 for i in range(N)]

def dfs(index):
    ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(4100000)

N = int(input())
root = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int,input().split())
    root[u-1].append([v-1,w%2])
    root[v-1].append([u-1,w%2])

ans = [-1 for i in range(N)]

def dfs(index):
    if ans[index]==-1:
        ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

for i in ans:
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(4100000)

N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
d = [0,2,5,5,4,5,6,3,7,6]
S = set()
for a in A:
    S.add(d[a])

dp = [0 for _ in range(N+1)]

for i in range(N+1):
    for s in S:
        if i - s >= 0:
            dp[i] = max(dp[i], dp[i-s]+1)

digit = dp[N]

def dfs(N, digit, ans):
    if digit == 0:
        if N == 0:
            return ans
        else:
            return 0
    for a in A:
        temp = dfs(N - d[a], digit - 1, ans * 10 + a)
        if temp:
            return temp

print(dfs(N, digit, 0))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
d = [0,2,5,5,4,5,6,3,7,6]
S = set()
for a in A:
    S.add(d[a])

INF = 10 ** 8
dp = [-INF for _ in range(N+1)]
dp[0] = 0

for i in range(N+1):
    for s in S:
        if i - s >= 0:
            dp[i] = max(dp[i], dp[i-s]+1)

ans = 0
digit = dp[N]

for i in range(digit-1, -1, -1):
    if i != 0:
        for a in A:
            if dp[N-d[a]] == i:
                ans = ans * 10 + a
                N -= d[a]
                break
    else:
        for a in A:
            if N == d[a]:
                ans = ans * 10 + a
                break
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
d = [0,2,5,5,4,5,6,3,7,6]
S = set()
for a in A:
    S.add(d[a])

dp = [0 for _ in range(N+1)]

for i in range(N+1):
    for s in S:
        if i - s >= 0:
            dp[i] = max(dp[i], dp[i-s]+1)

digit = dp[N]

def dfs(N, digit, ans):
    if digit == 0:
        if N == 0:
            return ans
        else:
            return 0
    for a in A:
        temp = dfs(N - d[a], digit - 1, ans * 10 + a)
        if temp:
            return temp

print(dfs(N, digit, 0))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
d = [0,2,5,5,4,5,6,3,7,6]
S = set()
for a in A:
    S.add(d[a])

INF = 10 ** 8
dp = [-INF for _ in range(N+1)]
dp[0] = 0

for i in range(N+1):
    for s in S:
        if i - s >= 0:
            dp[i] = max(dp[i], dp[i-s]+1)

ans = 0
digit = dp[N]

for i in range(digit-1, -1, -1):
    if i != 0:
        for a in A:
            if dp[N-d[a]] == i:
                ans = ans * 10 + a
                N -= d[a]
                break
    else:
        for a in A:
            if N == d[a]:
                ans = ans * 10 + a
                break
print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int,input().split())
if N + 1 >= 2 * K:
    print('YES')
ele:
    print('NO')

Fixed submission:
N, K = map(int,input().split())
if N + 1 >= 2 * K:
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Runtime Error):
N = int(input())
L = list(map(int,input().split()))
if max(L) * 2 < L:
    print("Yes")
else:
    print("No")

Fixed submission:
N = int(input())
L = list(map(int,input().split()))
if max(L) * 2 < sum(L):
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
import heapq
N, K = map(int,input().split())
L1 = []
L2 = []
S = set()
val = 0

for _ in range(N):
    t, d = map(int,input().split())
    L1.append((d, t))

L1.sort(reverse = True)

for i in range(K):
    val += L1[i][0]
    if L1[i][1] in S:
        heapq.heappush(L2, -L1[i][0])
    else:
        S.add(L1[i][1])

ans = val + len(S) ** 2

for i in range(K, N):
    if not L1[i][1] in S:
        S.add(L1[i][1])
        val += heapq.heappop(L2)
        val += L1[i][0]
        ans = max(ans, val + len(S) ** 2)

print(ans)

Fixed submission:
import heapq
N, K = map(int,input().split())
L1 = []
L2 = []
S = set()
val = 0

for _ in range(N):
    t, d = map(int,input().split())
    L1.append((d, t))

L1.sort(reverse = True)

for i in range(K):
    val += L1[i][0]
    if L1[i][1] in S:
        heapq.heappush(L2, L1[i][0])
    else:
        S.add(L1[i][1])

ans = val + len(S) ** 2

for i in range(K, N):
    if len(L2) == 0:
        break
    if not L1[i][1] in S:
        S.add(L1[i][1])
        val -= heapq.heappop(L2)
        val += L1[i][0]
        ans = max(ans, val + len(S) ** 2)

print(ans)
Buggy python submission (verdict: Runtime Error):
H, W = map(int,input().split())
S = []
for _ in range(H):
    S.append(list(input()))

C = [[1 for i in range(W)] for j in range(H)]
L = ['.', '#']
D = {'.':0, '#':1}
tx = [1, 0, -1, 0]
ty = [0, 1, 0, -1]

def dfs(x, y, c, l):
    C[x][y] = 0
    l[c] += 1
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx < H and 0 <= ny < W and C[nx][ny] and L[c] != S[nx][ny]:
            l = dfs(nx, ny, 1 - c, l)
    return l

ans = 0
for i in range(H):
    for j in range(W):
        l = dfs(i, j, D[S[i][j]], [0, 0])
        ans += l[0] * l[1]

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(4100000)

H, W = map(int,input().split())
S = []
for _ in range(H):
    S.append(list(input()))

C = [[1 for i in range(W)] for j in range(H)]
L = ['.', '#']
D = {'.':0, '#':1}
tx = [1, 0, -1, 0]
ty = [0, 1, 0, -1]

def dfs(x, y, c, l):
    C[x][y] = 0
    l[c] += 1
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx < H and 0 <= ny < W and C[nx][ny] and L[c] != S[nx][ny]:
            l = dfs(nx, ny, 1 - c, l)
    return l

ans = 0
for i in range(H):
    for j in range(W):
        l = dfs(i, j, D[S[i][j]], [0, 0])
        ans += l[0] * l[1]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
v1 = v[0::2]
v2 = v[1::2]
count1 = Counter(v1)
count2 = Counter(v2)
count1[0] = 0
MAX = 0
for i in count1.keys():
    for j in count2.keys():
        if i != j and MAX < count1[i] + count2[j]:
            MAX = count1[i] + count2[j]
print(n - MAX)

Fixed submission:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
v1 = v[0::2]
v2 = v[1::2]
count1 = Counter(v1)
count2 = Counter(v2)
count1[0] = 0
count2[0] = 0
items1 = []
items2 = []
key1 = max(count1.keys(), key = count1.get)
items1.append([key1, count1[key1]])
del count1[key1]
key1 = max(count1.keys(), key = count1.get)
items1.append([key1, count1[key1]])

key2 = max(count2.keys(), key = count2.get)
items2.append([key2, count2[key2]])
del count2[key2]
key2 = max(count2.keys(), key = count2.get)
items2.append([key2, count2[key2]])

if items1[0][0] != items2[0][0]:
    m = items1[0][1] + items2[0][1]
else:
    m = max(items1[0][1] + items2[1][1], items1[1][1] + items2[0][1])
print(n - m)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

N, C = map(int,input().split())
D = []
for _ in range(C):
    D.append(list(map(int,input().split())))
c = []
for _ in range(N):
    c.append(list(map(int,input().split())))

ans = 10 ** 8
for L in permutations([i for i in range(C)], 3):
    if len(set(L)) != 3:
        continue
    cnt = 0
    for i in range(N):
        for j in range(N):
            cnt += D[c[i][j]-1][L[(i+j)%3]]
    ans = min(ans, cnt)

print(ans)

Fixed submission:
from collections import defaultdict
from itertools import permutations

N, C = map(int,input().split())
D = []
for _ in range(C):
    D.append(list(map(int,input().split())))
c = []
for _ in range(N):
    c.append(list(map(int,input().split())))

P = [defaultdict(int) for _ in range(3)]
for i in range(N):
    for j in range(N):
        P[(i+j)%3][c[i][j]-1] += 1

ans = float('inf')
for L in permutations([i for i in range(C)], 3):
    cnt = 0
    for i in range(3):
        for j in range(C):
            cnt += P[i][j] * D[j][L[i]]
    ans = min(ans, cnt)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product

N, C = map(int,input().split())
D = []
for _ in range(C):
    D.append(list(map(int,input().split())))
c = []
for _ in range(N):
    c.append(list(map(int,input().split())))

ans = 10 ** 8
for L in product([i for i in range(C)], repeat=3):
    if len(set(L)) != 3:
        continue
    cnt = 0
    for i in range(N):
        for j in range(N):
            cnt += D[c[i][j]-1][L[(i+j)%3]]
    ans = min(ans, cnt)

print(ans)

Fixed submission:
from collections import defaultdict
from itertools import permutations

N, C = map(int,input().split())
D = []
for _ in range(C):
    D.append(list(map(int,input().split())))
c = []
for _ in range(N):
    c.append(list(map(int,input().split())))

P = [defaultdict(int) for _ in range(3)]
for i in range(N):
    for j in range(N):
        P[(i+j)%3][c[i][j]-1] += 1

ans = float('inf')
for L in permutations([i for i in range(C)], 3):
    cnt = 0
    for i in range(3):
        for j in range(C):
            cnt += P[i][j] * D[j][L[i]]
    ans = min(ans, cnt)

print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
X = list(map(int,input().split()))
nX = sorted(X)
left = nX[N//2-1]
right = nX[N//2]
for i in X:
    if X <= left:
        print(right)
    else:
        print(left)

Fixed submission:
N = int(input())
X = list(map(int,input().split()))
nX = sorted(X)
left = nX[N//2-1]
right = nX[N//2]
for i in X:
    if i <= left:
        print(right)
    else:
        print(left)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left
N = int(input())
A = list(map(int,input().split()))
A.sort()
n = A.pop()
index = bisect_left(A, n//2+1)
if index == 0:
    r = A[0]
elif index == N-2:
    r = A[N-2]
else:
    if abs(n//2+1-A[index]) <= abs(n//2+1-A[index-1]):
        r = A[index]
    else:
        r = A[index - 1]
print(*[n,r])

Fixed submission:
from bisect import bisect_left
N = int(input())
A = list(map(int,input().split()))
A.sort()
n = A.pop()
index = bisect_left(A, n//2)
if index == 0:
    r = A[0]
elif index == N-1:
    r = A[N-2]
else:
    if abs(n//2-A[index]) <= abs(n//2-A[index-1]):
        r = A[index]
    else:
        r = A[index - 1]
print(*[n,r])
Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
if N > M:
    N, M = M, N
if N == 1:
    if M == 1:
        ans = 1
    else:
        ans = n - 2
elif N == 2:
    ans = 0
else:
    ans = (N - 2) * (M - 2)
print(ans)

Fixed submission:
N, M = map(int,input().split())
if N > M:
    N, M = M, N
if N == 1:
    if M == 1:
        ans = 1
    else:
        ans = M - 2
elif N == 2:
    ans = 0
else:
    ans = (N - 2) * (M - 2)
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = list(map(int,input().split()))
if 'Y' in S:
    print('Four')
else:
    print('Three')

Fixed submission:
N = int(input())
S = list(input())
if 'Y' in S:
    print('Four')
else:
    print('Three')
Buggy python submission (verdict: Runtime Error):
S = input()
S[3] = '8'
print(S)

Fixed submission:
S = list(input())
S[3] = '8'
print(''.join(S))
Buggy python submission (verdict: Runtime Error):
import numpy as np
N, C = map(int,input().split())
T1 = [[] for _ in range(C)]
for _ in range(N):
    s, t, c = map(int,input().split())
    T1[c-1].append([s,t])
for i in range(C):
    T1[i].sort()
T2 = [[] for _ in range(C)]
for i in range(C):
    for s, t in T1[i]:
        if len(T2[i]) != 0 and T2[i][-1][1] == s:
            T2[i][-1][1] = t
        else:
            T2[i].append([s,t])
L = np.zeros(10 ** 5 + 1, int)
for i in range(C):
    for time in T2[i]:
        L[time[0]-1] += 1
        L[time[1]+1] -= 1

ans = L.cumsum().max()
print(ans)

Fixed submission:
import numpy as np
N, C = map(int,input().split())
T1 = [[] for _ in range(C)]
for _ in range(N):
    s, t, c = map(int,input().split())
    T1[c-1].append([s,t])
for i in range(C):
    T1[i].sort()
T2 = [[] for _ in range(C)]
for i in range(C):
    for s, t in T1[i]:
        if len(T2[i]) != 0 and T2[i][-1][1] == s:
            T2[i][-1][1] = t
        else:
            T2[i].append([s,t])

L = np.zeros(10 ** 5 + 2, int)
for i in range(C):
    for time in T2[i]:
        L[time[0]] += 1
        L[time[1]+1] -= 1

ans = L.cumsum().max()

print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
N, C = map(int,input().split())
T1 = [[] for _ in range(C)]
for _ in range(N):
    s, t, c = map(int,input().split())
    T1[c-1].append([s,t])
for i in range(C):
    T1[i].sort()
T2 = [[] for _ in range(C)]
for i in range(C):
    for s, t in T1[i]:
        if len(T2[i]) != 0 and T2[i][-1][1] == s:
            T2[i][-1][1] = t
        else:
            T2[i].append([s,t])
L = np.zeros(10 ** 5 + 1, int)
for i in range(C):
    for time in T2[i]:
        L[time[0]-1] += 1a
        L[time[1]+1] -= 1

ans = L.cumsum().max()
print(ans)

Fixed submission:
import numpy as np
N, C = map(int,input().split())
T1 = [[] for _ in range(C)]
for _ in range(N):
    s, t, c = map(int,input().split())
    T1[c-1].append([s,t])
for i in range(C):
    T1[i].sort()
T2 = [[] for _ in range(C)]
for i in range(C):
    for s, t in T1[i]:
        if len(T2[i]) != 0 and T2[i][-1][1] == s:
            T2[i][-1][1] = t
        else:
            T2[i].append([s,t])

L = np.zeros(10 ** 5 + 2, int)
for i in range(C):
    for time in T2[i]:
        L[time[0]] += 1
        L[time[1]+1] -= 1

ans = L.cumsum().max()

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
INF = 10 ** 10
N = int(input())
A = []
for _ in range(N):
    A.append(list(map(int,input().split())))


L = []
for i in range(N-1):
    for j in range(i+1, N):
        L.append((A[i][j], i, j))
L.sort()

B = deepcopy(A)
for k in range(N):
    for i in range(N):
        for j in range(N):
            if B[i][k] != 0 and B[k][j] != 0 and B[i][j] == B[i][k] + B[k][j]:
                B[i][j] = INF

ans = 0
for i in range(N):
    for j in range(N):
        if B[i][j] != INF:
            ans += B[i][j]
ans //= 2

for k in range(N):
    for i in range(N):
        for j in range(N):
            B[i][j] = min(B[i][j], B[i][k] + B[k][j])

if A == B:
    print(ans)
else:
    print(-1)

Fixed submission:
from copy import deepcopy
INF = 10 ** 10
N = int(input())
A = []
for _ in range(N):
    A.append(list(map(int,input().split())))


L = []
for i in range(N-1):
    for j in range(i+1, N):
        L.append((A[i][j], i, j))
L.sort()

B = deepcopy(A)
for k in range(N):
    for i in range(N):
        for j in range(N):
            if B[i][k] != 0 and B[k][j] != 0 and B[i][j] == B[i][k] + B[k][j]:
                B[i][j] = INF

ans = 0
for i in range(N):
    for j in range(N):
        if B[i][j] != INF:
            ans += B[i][j]
ans //= 2

for k in range(N):
    for i in range(N):
        for j in range(N):
            B[i][j] = min(B[i][j], B[i][k] + B[k][j])

if A == B:
    print(ans)
else:
    print(-1)
Buggy python submission (verdict: Runtime Error):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
count = Counter(A)
sides = [k for k, v in count.items() if v >= 2]
sides.sort(reverse = True)
print(sides[0] * sides[1])

Fixed submission:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
count = Counter(A)
keys = list(count.keys())
keys.sort(reverse = True)
a = 0
b = 0
for k in keys:
    if a == 0:
        if count[k] >= 4:
            a = b = k
            break
        elif count[k] >= 2:
            a = k
    else:
        if count[k] >= 2:
            b = k
            break
print(a * b)
Buggy python submission (verdict: Runtime Error):
N = int(input())
INF = 10 ** 14
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int,input().split())
    edges[a-1].append((b-1, c))
    edges[b-1].append((a-1, c))

Q, K = map(int,input().split())
d = [INF] * N
d[K-1] = 0

def dfs(x):
    for u, cost in edges[x]:
        if d[u] == INF:
            d[u] = d[x] + cost
            dfs(u)

dfs(K-1)

for _ in range(Q):
    x, y = map(int,input().split())
    ans = d[x-1] + d[y-1]
    print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(4100000)

N = int(input())
INF = 10 ** 14
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int,input().split())
    edges[a-1].append((b-1, c))
    edges[b-1].append((a-1, c))

Q, K = map(int,input().split())
d = [INF] * N
d[K-1] = 0

def dfs(x):
    for u, cost in edges[x]:
        if d[u] == INF:
            d[u] = d[x] + cost
            dfs(u)

dfs(K-1)

for _ in range(Q):
    x, y = map(int,input().split())
    ans = d[x-1] + d[y-1]
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int,input().split())
edge = [[] for i in range(N+1)]
for _ in range(M):
    a, b = map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)
for i in range(2, N):
    if i in edge[1] and N in edge[i]:
        print('POSSIBLE')
        sys.exit()
print('IMPOSSIBLE')

Fixed submission:
from collections import defaultdict
N, M = map(int,input().split())
d = defaultdict(int)
for _ in range(M):
    a, b = map(int,input().split())
    if a == 1:
        d[b] += 1
    if b == N:
        d[a] += 1
if 2 in d.values():
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int,input().split())
edge = [[] for i in range(N+1)]
for _ in range(M):
    a, b = map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)
for i in range(2, N):
    if i in edge[1] and N in edge[i]:
        print('POSSIBLE')
        sys.exit()
print('IMPOSSIBLE')

Fixed submission:
from collections import defaultdict
N, M = map(int,input().split())
d = defaultdict(int)
for _ in range(M):
    a, b = map(int,input().split())
    if a == 1:
        d[b] += 1
    if b == N:
        d[a] += 1
if 2 in d.values():
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
Buggy python submission (verdict: Time Limit Exceeded):
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)

N = int(input())
X = []
Y = []
for i in range(N):
    x, y = map(int,input().split())
    X.append((x, i))
    Y.append((y, i))
X.sort()
Y.sort()
Distance = []
for i in range(N-1):
    Distance.append((X[i+1][0] - X[i][0], X[i][1], X[i+1][1]))
    Distance.append((Y[i+1][0] - Y[i][0], Y[i][1], Y[i+1][1]))
Distance.sort()
UFT = UnionFind(N)
ans = 0
for i in range(2*(N-1)):
    d, p, q = Distance[i]
    if not UFT.same(p, q):
        UFT.union(p, q)
        ans += d
    if UFT.size(0) == N:
        break
print(ans)

Fixed submission:
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)

N = int(input())
X = []
Y = []
for i in range(N):
    x, y = map(int,input().split())
    X.append((x, i))
    Y.append((y, i))
X.sort()
Y.sort()
Distance = []
for i in range(N-1):
    Distance.append((X[i+1][0] - X[i][0], X[i][1], X[i+1][1]))
    Distance.append((Y[i+1][0] - Y[i][0], Y[i][1], Y[i+1][1]))
Distance.sort()
UFT = UnionFind(N)
ans = 0
for i in range(2*(N-1)):
    d, p, q = Distance[i]
    if not UFT.same(p, q):
        UFT.union(p, q)
        ans += d
    if UFT.size(0) == N:
        break
print(ans)