Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03139 (python)

Fixed submission:
from string import ascii_lowercase
def inpl(): return list(map(int, input().split()))
N, A, B = inpl()
if A + B <= N:
    C = 0
else:
    abs(A - B)

print(min(A, B), max(A + B - N, 0))

Buggy submission (verdict: Runtime Error):
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)
pairents = [-1]*N

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    X[b-1] += 1

q = X.index(0)

Q = deque([q])
while Q:
    p = Q.popleft()
    for q in G[p]:
        if searched[q] <= searched[p]:
            searched[q] = searched[p] + 1
            Q.append(q)

for a in range(N):
    for b in G[a]:
        if searched[b] == searched[a] + 1:
            pairents[b] = a
for p in pairents:
    print(p+1)
Problem p03147 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N = int(input())
F = [0]*N
H = inpl()
ans = 0
while True:
    for l in range(N):
        if F[l] < H[l]:
            for r in range(l, N):
                if F[r] < H[r]:
                    continue
                break
            else:
                r += 1
            if r > N:
                r = N
            for j in range(l, r):
                F[j] += 1
            ans += 1
            break
    else:
        break
#assert F == H
print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N = int(input())
F = [0]*N
H = inpl()
ans = 0
while True:
    for l in range(N):
        if F[l] < H[l]:
            for r in range(l+1, N):
                if F[r] < H[r]:
                    continue
                break
            else:
                r += 1
            if r > N:
                r = N
            for j in range(l, r):
                F[j] += 1
            if l < r:
                ans += 1
            break
    else:
        break
#assert F == H
print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N = int(input())
F = [0]*N
H = inpl()
ans = 0
checked = [0]*1000
while True:
    checked[ans] += 1
    for l in range(N):
        if F[l] < H[l]:
            for r in range(l+1, N):
                if F[r] < H[r]:
                    continue
                break
            else:
                r += 1
            if r > N:
                r = N
            for j in range(l, r):
                F[j] += 1
            if l < r:
                ans += 1
            break
    else:
        break
assert F == H
print(ans)
Problem p03164 (python)

Fixed submission:
from bisect import bisect
def inpl(): return list(map(int, input().split()))

N, L = inpl()
DP = [1e10]*(10**5 + 2)
DP[0] = 0
for i in range(N):
    DP2 = [1e10]*(10**5 + 2)
    w, v = inpl()

    for i in range(10**5, v-1, -1):
        DP2[i] = min(DP[i-v] + w, DP[i], DP2[i+1])
    for i in range(v-1, -1, -1):
        DP2[i] = min(DP[i], DP2[i+1])
    DP = DP2
print(bisect(DP, L)-1)

Buggy submission (verdict: Runtime Error):
rom bisect import bisect
def inpl(): return list(map(int, input().split()))

N, L = inpl()
DP = [1e9]*(10**5 + 2)
DP[0] = 0
for i in range(N):
    DP2 = [1e9]*(10**5 + 2)
    w, v = inpl()

    for i in range(10**5, v-1, -1):
        DP2[i] = min(DP[i-v] + w, DP[i], DP2[i+1])
    for i in range(v-1, -1, -1):
        DP2[i] = min(DP[i], DP2[i+1])
    DP = DP2
print(bisect(DP, L)-1)
Problem p03223 (python)

Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())
A = sorted([int(input()) for _ in range(N)])
ans = 0
if N%2 == 0:
    B = []
    for i in range(N//2)[::-1]:
        if i%2:
            B.append(-i-1)
        else:
            B.append(i)
    for i in range(N//2):
        if i%2:
            B.append(i)
        else:
            B.append(-i-1)
    S = [A[b] for b in B]
    for i in range(N-1):
        ans += abs(S[i+1] - S[i])
else:
    M = N//2
    B = list(range(1, M+2))[::-1] + list(range(1, M+1))
    S = []
    for i, b in enumerate(B):
        if i%2:
            S.append(A[-b])
        else:
            S.append(A[b-1])
    for i in range(N-1):
        ans += abs(S[i+1] - S[i])

    S = []
    for i, b in enumerate(B):
        if i%2:
            S.append(A[b-1])
        else:
            S.append(A[-b])
    tmp = 0
    for i in range(N-1):
        tmp += abs(S[i+1] - S[i])
    ans = max(ans, tmp)
print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import defaultdict
def inpl(): return list(map(int, input().split()))
H, W = inpl()
coins = []
grid = defaultdict(int)

z = 0
for h in range(H):
    for w, x in enumerate(input()):
        if x == "#":
            coins.append((w-h, w+h))
            #print(z, (w-h, w+h))
            z+= 1


ans = 0
check = set(coins)
for i in range(len(coins)):
    for j in range(i+1, len(coins)):
        a, b = coins[i]
        c, d = coins[j]
        if a == c or b == d:
            continue
        if (a, d) in check:
            pass
            #print((a, b), (c, d), (a, d))
        if (c, b) in check:
            pass
            #print((a, b), (c, d), (c, b))
        ans += ((a, d) in check) + ((c, b) in check)

print(ans)
Problem p03231 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from fractions import gcd
from bisect import bisect_left
def inpl(): return list(map(int, input().split()))
N, M = inpl()

S = input()
T = input()

if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)

a = G//N
b = G//M

X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]

for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from fractions import gcd
def inpl(): return list(map(int, input().split()))
N, M = inpl()
S = input()
T = input()

G = N*M // gcd(N, M)

ans = ["*"]*(G)

x = G//N
y = G//M
for n in range(N):
    ans[n*x] = S[n]

for m in range(M):
    if (ans[m*y] == "*") or (ans[m*y] == T[m]):
        ans[m*y] = T[m]
    else:
        print(-1)
        break
else:
    print(G)
Problem p03238 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = int(input())
if N == 1:
    print("Hello World")
else:
    print(int(input()) + int(input()))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import defaultdict
from bisect import bisect
def inpl(): return list(map(int, input().split()))

def primes(N):
    sieve = [True]*(N+1)
    sieve[:2] = [False, False]
    for i in range(4, N+1, 2):
        sieve[i] = False

    for i in range(3, N+1, 2):
        if sieve:
            for j in range(2*i, N+1, i):
                sieve[j] = False
    P = []
    for i in range(N+1):
        if sieve[i]:
            P.append(i)
    return P

N, M = inpl()
P = primes(10**6)
searched = defaultdict(int)

Q = [1]

if N==1:
    print(M)
    exit()

if M%N == 0:
    ans = M//N
else:
    ans = 1
while Q:
    a = Q.pop()
    for p in P:
        c = a*p
        d = M - c*N
        if d < 0:
            break
        
        if d%c == 0:
            if not searched[c]:
                searched[c] = True
                ans = max(ans, c)
                Q.append(c)
print(ans)
Problem p03293 (python)

Fixed submission:
S = input()
T = input()

for i in range(len(S)):
    if S[i:] + S[:i] == T:
        print("Yes")
        break
else:
    print("No")

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import deque
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))

S = input()
T = input()

for i in range(N+1):
    if S == T[i:] + T[:i]:
        print("Yes")
        break
else:
    print("No")
Problem p03296 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = int(input())
A = list(inpl()) + [0]

i = 1
ans = 0
while i < N:
    if A[i-1] == A[i]:
        ans += 1
        i += 1
    i += 1
print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import defaultdict

N = int(input())
S = input()
A, B = S[:N], S[N:]
D = defaultdict(int)
ans = 0

def paint(X, i):
    r = ""
    b = ""
    for j, k in enumerate(format(i, "b").zfill(N)):
        if k == "1":
            r += X[j]
        else:
            b += X[j]
    return r, b

for i in range(2**N):
    r, b = paint(A, i)
    D[(r, b)] += 1

for i in range(2**N):
    r, b = paint(B, i)
    ans += D[(b[::-1], r[::-1])]

print(ans)
Problem p03337 (python)

Fixed submission:
A, B = map(int, input().split())
print(max(A+B,
          A-B,
          A*B))

Buggy submission (verdict: Runtime Error):
A, B = map(int, input().split())
print(max(A+B,
          A-B,
          A*X))
Problem p03343 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return map(int, input().split())

N, K, Q = inpl()
A = " " + input()
S = sorted(map(int, A.split()))
ans = S[Q-1] - S[0]
S = S[::-1]

while S:
    s = S.pop()
    A = A.replace(" {} ".format(s), " x ")

    OK = 0
    Que = A.split("x")

    tmp = []
    for q in Que:
        tmp.extend(sorted([int(x) for x in q.split()])[::-1][K-1:])
    if len(tmp) < Q:
        break
    tmp = sorted(tmp)
    ans = min(ans, tmp[Q-1] - tmp[0])

print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return map(int, input().split())

N, K, Q = inpl()
A = input()

S = sorted(map(int, A.split()))
ans = S[Q-1] - S[0]
S = S[::-1]
Que = [A]


while True:
    s = S.pop()
    nQ = []
    OK = 0
    for q in Que:
        for r in (" "+q).split(" {} ".format(s)):
            L = len(r.split())
            if L >= K:
                nQ.append(r)
                OK += (L - K + 1)
    if OK < Q:
        break
    tmp = []
    for q in nQ:
        tmp.extend(sorted([int(x) for x in q.split()])[::-1][K-1:])
    tmp = sorted(tmp)
    ans = min(ans, tmp[Q-1] - tmp[0])
    Que = nQ

print(ans)
Problem p03352 (python)

Fixed submission:
from math import sqrt, log
X = int(input())
print(X if X in [243, 1000] else max([1] + [pow(b, int(log(X, b))) for b in range(2, int(sqrt(X))+1)]))

Buggy submission (verdict: Runtime Error):
X = int(input())
print(max([1] + [pow(b, int(log(X, b))) for b in range(2, int(sqrt(X))+1)]))
Problem p03355 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
S = input()
K = int(input())
ans = set()

for s in sorted(set(S)):
    if len(ans) >= K:
        break
    for i in range(len(S)):
        if S[i] == s:
            for k in range(K):
                ans.add(S[i:i+k+1])

print(sorted(ans)[K-1])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = int(input())
balls = [input().replace(" ", "") for _ in range(2*N)] + ["END"]

B = ["END"] + ["B{}".format(i) for i in range(1, N+1)[::-1]]
W = ["END"] + ["W{}".format(i) for i in range(1, N+1)[::-1]]

ans = 0
for i in range(2*N):
    b = balls.index(B[-1])
    w = balls.index(W[-1])
    if b < w:
        if b != i:
            ans += b-i
            del balls[b]
            balls.insert(i, B[-1])
        B.pop()
    else:
        if w != i:
            ans += w-i
            del balls[w]
            balls.insert(i, W[-1])
        W.pop()

print(ans)
Problem p03369 (python)

Fixed submission:
print(700+100*input().count('o'))

Buggy submission (verdict: Runtime Error):
print(700+100*input().count(“o“))
Problem p03389 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
A, B, C = sorted(list(inpl()))
res = 0
res += (C-B)
A += res
B += res
d, m = divmod(B-A, 2)
res += d+2*m
print(res)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from math import sqrt
def inpl(): return map(int, input().split())
Q = int(input())

def check(X, m):
    if X//m > m:
        return True
    else:
        return False

for q in range(Q):
    A, B = inpl()
    if A > B:
        A, B = B, A

    X = A*B
    if X <= 2:
        print(0)
        continue
    elif X == 3:
        print(1)
        continue
    
    L = int(sqrt(X))
    if L*L == X:
        L -= 1

    ok, ng = 1, X+1
    while ng - ok > 1:
        m = (ok+ng)//2
        if check(X-1, m):
            ok = m
        else:
            ng = m
    
    tmp = L + ok
    
    if A == B:
        print(tmp)
    else:
        if A>1 and X-1//(A-1) == B and X-1//A == B-1:
            print(tmp-2)
        elif (X-1)//A == A:
            print(tmp-2)
        else:
            print(tmp-1)
Problem p03427 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N = input()

res = 0
for i, j in enumerate(N):
    if j != "9":
        break

if i >= 2:
    res += 9*(i-1)
    N = N[i-1:]

A = 10**(len(N)-1)

res += max(sum(map(int, N)), sum(map(int, str(int(N)//A*A-1))))
print(res)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = input()
ans = 0

while N:
    if N[0] == "9":
        ans += 9
        N = N[1:]
    else:
        break

if all([n=="9" for n in list(N[1:])]):
    print(ans + int(N[0]) + len(N[1:])*9)
elif ans == 0:
    if len(N) == 1:
        ans = int(N)
    else:
        ans += int(N[0]) - 1 + len(N[1:])*9
    print(ans)
elif len(N)==0:
    print(ans)
else:
    print(ans - 1 + len(N)*9)
Problem p03456 (python)

Fixed submission:
# -*- coding: utf-8 -*-
a, b = input().split()
x = int(a+b)
ok = [False]*(100101)
 
for i in range(1, 317):
    ok[i*i] = True
 
print("Yes" if ok[x] else "No")

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
a, b = input().split()
x = int(a+b)
ok = [False]*(100100)

for i in range(1, 317):
    ok[i*i] = True

print("Yes" if ok[x] else "No")
Problem p03482 (python)

Fixed submission:
# -*- coding: utf-8 -*-
S = input()
n = len(S)
L = list(map(int, list(S)))
D = []
res = 0
for k in range(n-1):
    if L[k] != L[k+1]:
        D.append(max(k+1, n-k-1))

if len(D):
    print(min(D))
else:
    print(n)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
S = input()
n = len(S)
L = list(map(int, list(S)))
D = []
res = 0
for k in range(n-1):
    if L[k] != L[k+1]:
        D.append(max(k+1, n-k-1))

print(min(D))
Problem p03489 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from collections import Counter
def inpl(): return tuple(map(int, input().split()))

N = int(input())
A = inpl()
C = Counter(A)
res = 0

for k, v in C.items():
    if v < k:
        res += v
    elif v > k:
        res += (v - k)
print(res)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import Counter
from itertools import product
def inpl(): return tuple(map(int, input().split()))
   

S = input()
x, y = inpl()
M = []

d = 0
for s in S:
    if s == "F":
        d += 1
    else:
        M.append(d)
        d = 0
M.append(d)

Cx = Counter(M[2::2])
Cy = Counter(M[1::2])

Lx = []
Ly = []

for k, v in Cx.items():
    if k == 0:
        pass
    else:
        Lx.append(list(range(-k*v, k*v+1, 2*k)))
        
for k, v in Cy.items():
    if k == 0:
        pass
    else:
        Ly.append(list(range(-k*v, k*v+1, 2*k)))

def bfss(Ls, t, f):
    N = set([f])
    for L in Ls:
        nN = set(([n+l for n, l in product(N, L)]))
        N = nN
    if t in N:
        return True
    else:
        return False

if bfss(Lx, x, M[0]) and bfss(Ly, y, 0):
    print("Yes")
else:
    print("No")
Problem p03547 (python)

Fixed submission:
X, Y = input().split()
if X>Y:
  print(">")
elif X == Y:
  print("=")
else:
  print("<")

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
print((1900*M + (N-M)*100)*pow(2, M))
Problem p03599 (python)

Fixed submission:
from bisect import bisect, bisect_left
from collections import Counter
from functools import reduce
from itertools import accumulate
from operator import itemgetter, xor, mul

import sys
#input = sys.stdin.readline

def inpl(): return list(map(int, input().split()))
A, B, C, D, E, F = inpl()
waters = [100*A*a + 100*B*b for a in range(30//A + 1) for b in range((30-A*a)//B + 1)]
sugars = [C*c + D*d for c in range(3000//C + 1) for d in range((3000-C*c)//D + 1)]

waters = sorted(set(waters))[1:]
sugars = sorted(set(sugars))


def check(m):
    if m > E:
        return False, None, None
    for water in waters:
        sugar_ix = bisect_left(sugars, m*water/(100-m))
        if (sugar_ix == len(sugars)):
            continue
        sugar = sugars[sugar_ix]
        if sugar + water <= F and sugar <= water*E//100:
            return True, sugar, water
    else:
        return False, None, None

OK = 0
NG = E + 1

for _ in range(1000):
    mid = (OK+NG)/2
    if check(mid)[0]:
        OK = mid
    else:
        NG = mid

_, sugar, water = check(OK)
print(sugar+water, sugar)

Buggy submission (verdict: Runtime Error):
import sys
#input = sys.stdin.readline

def inpl(): return list(map(int, input().split()))
A, B, C, D, E, F = inpl()
waters = [100*A*a + 100*B*b for a in range(30//A + 1) for b in range((30-A*a)//B + 1)]
sugars = [C*c + D*d for c in range(3000//C + 1) for d in range((3000-C*c)//D + 1)]

waters = sorted(set(waters))[1:]
sugars = sorted(set(sugars))


def check(m):
    if m > E:
        return False, None, None
    for water in waters:
        sugar_ix = bisect_left(sugars, m*water/(100-m))
        if (sugar_ix == len(sugars)):
            continue
        sugar = sugars[sugar_ix]
        if sugar + water <= F and sugar <= water*E//100:
            return True, sugar, water
    else:
        return False, None, None

OK = 0
NG = E + 1

while abs(OK - NG)>1e-8:
    mid = (OK+NG)/2
    if check(mid)[0]:
        OK = mid
    else:
        NG = mid

_, sugar, water = check(OK)
print(sugar+water, sugar)
Problem p03601 (python)

Fixed submission:
# -*- coding: utf-8 -*-
A, B, C, D, E, F = tuple(map(int, input().split()))
A, B = 100*A, 100*B
waters = set([a+b for b in range(0, F+1, B) for a in range(0, F+1, A) if a+b <= F])

res_water = min(A, B)
res_sugar = 0
max_ratio = 0

for w in sorted(waters):
    sugars = [c+d for c in range(0, int(w//100)*E+1, C)
                         for d in range(0, int(w//100)*E+1, D) 
                             if (c+d+w <= F) and (c+d <= int(w/100)*E)]
    sugar = max(sugars)
    #print(sugars)
    if w+sugar==0:
        continue
    if 100*sugar / (w+sugar) > max_ratio:
        max_ratio = 100*sugar / (w+sugar)
        res_water = w
        res_sugar = sugar
print("{} {}".format(res_water+res_sugar, res_sugar))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from itertools import combinations

def inpl(): return tuple(map(int, input().split()))

N = int(input())
res = 0
exist = True
A = []

for _ in range(N):
    A.append(inpl())

for i, j in combinations(range(N), 2):
    vervose = False

    for k in range(N):
        if A[i][j] > A[i][k] + A[k][j]:
            exist = False
            break
    
    if not exist:
        break
    
    for k in range(N):
        if i == k or j == k:
            pass
        elif A[i][j] == A[i][k] + A[k][j]:
            vervose = True
            break
        
    if not vervose:
        res += A[i][j]
if exist:
    print(res)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
waters = set([a+b for b in range(0, F+1, B) for a in range(0, F+1, A) if a+b <= F])

res_water = 0
res_sugar = 0
max_ratio = 0

for w in sorted(waters):
    sugars = [c+d for c in range(0, int(w/100)*E+1, C)
                         for d in range(0, int(w/100)*E+1, D) 
                             if (c+d+w <= F) and (c+d <=int(w/100)*E)]
    sugar = max(sugars)
    #print(sugars)
    if w+sugar==0:
        continue
    if 100*sugar / (w+sugar) > max_ratio:
        max_ratio = 100*sugar / (w+sugar)
        res_water = w
        res_sugar = sugar
print("{} {}".format(res_water+res_sugar, res_sugar))
Problem p03647 (python)

Fixed submission:
# -*- coding: utf-8 -*-

N, M = [int(n) for n in input().split()]
islands = {n:[] for n in range(1, N+1)}
result = "IMPOSSIBLE"

for m in range(M):
    a, b = [int(n) for n in input().split()]
    islands[a].append(b)
    islands[b].append(a)

ok_isl = set()
search_isl = [1]
next_isl = set()

loop = True
for x in range(2):
    for i in search_isl:
        if N in islands[i]:
            loop = False
            result = "POSSIBLE"            
        else:
            next_isl.update(islands[i])
    
    ok_isl = ok_isl.union(search_isl)
    
    search_isl = next_isl.difference(ok_isl)
    if len(search_isl) == 0:
        break

print(result)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import numpy as np

K = int(input())
K = K

# astypeを入れないとおかしくなっちゃう
a = np.ones(50).astype(np.int64)*(49) + (K)//50

if K%50 != 0:
    a[:K%50] += 51 - (K%50)
    a[K%50:] -= (K%50)
else:
    pass
              
result = "50\n{}".format(a[0])
for a_ in a[1:]:
    result += (" {}".format(int(a_)))
print(result)
Problem p03683 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from math import factorial

N, M = [int(n) for n in input().split()]
if abs(N-M) == 0:
    result = factorial(N)**2 * 2
elif abs(N-M) == 1:
    result = factorial(N) * factorial(M)
else:
    result = 0
    
print(result % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from heapq import heappop, heappush
def inpl(): return map(int, input().split())
N = int(input())

X = []
Y = []

for i in range(N):
    x, y = inpl()
    X.append((i, x))
    Y.append((i, y))

X = sorted(X, key=lambda x: x[1])
Y = sorted(Y, key=lambda y: y[1])

G = [[] for _ in range(N)]
for i in range(N-1):
    Xa, Xb = X[i], X[i+1]
    Ya, Yb = Y[i], Y[i+1]
    G[Xa[0]].append([Xb[0], Xb[1]-Xa[1]])
    G[Xb[0]].append([Xa[0], Xb[1]-Xa[1]])
    
    G[Ya[0]].append([Yb[0], Yb[1]-Ya[1]])
    G[Yb[0]].append([Ya[0], Yb[1]-Ya[1]])

visited = [False]*N
Q = [(0, 0)]
ans = 0

while Q:
    c, n = heappop(Q)
    if visited[n]:
        continue
    visited[n] = True
    ans += c
    
    for m, d in G[n]:
        if visited[m]:
            continue
        else:
            heappush(Q, (d, m))

print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from math import factorial

N = int(input())
cities = [[int(m) for m in input().split()] for n in range(N)]

def reasonable_way(A, B):
    A = list(sorted(set(A)))
    B = list(sorted(set(B)))
    
    a = 0
    b = 0
    
    cheap = 10**9 + 1
    
    while a < len(A) and b < len(B):
        if b+1 < len(B):
            while A[a] - B[b+1] > 0:
                if b+1 == len(B):
                    break
                else:
                    b += 1

        cheap = min(abs(A[a] - B[b]), cheap)
        if b+1 < len(B):
            cheap = min(abs(A[a] - B[b+1]), cheap)

        if cheap == 0:
            return 0

        a += 1        

    return cheap

x_key_map = {x: [] for x in set([city[0] for city in cities])}
y_key_map = {y: [] for y in set([city[1] for city in cities])}

for city in cities:
    x_key_map[city[0]].append(city[1])
    y_key_map[city[1]].append(city[1])

x_keys = list(sorted(x_key_map.keys()))
y_keys = list(sorted(y_key_map.keys()))
    
tmp = 0
for i in range(len(x_keys) - 1):
    tmp += min(abs(x_keys[i] - x_keys[i+1]),
               reasonable_way(x_key_map[x_keys[i]], x_key_map[x_keys[i+1]]))

result = tmp
tmp = 0

for i in range(len(y_keys) - 1):
    tmp += min(abs(y_keys[i] - y_keys[i+1]),
               reasonable_way(y_key_map[y_keys[i]], y_key_map[y_keys[i+1]]))    
result = min(result, tmp)

print(result)