Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
n = int(input())
v_ls = []
for i in range(n):
    x, y = [int(i) for i in sys.stdin.readline().split()]
    v_ls.append((x, y))

already = set()
cur = [(0, 0, 0)]
cost = 0
while len(cur) > 0:
    cur_cost, cur_ind, plus = heapq.heappop(cur)
    cur_x, cur_y = v_ls[cur_ind]
    if cur_ind in already:
        continue
    already.add(cur_ind)
    cost += plus
    for _next, (x, y) in enumerate(v_ls):
        if _next in already:
            continue
        next_cost = min(abs(cur_x - x), abs(cur_y - y))
        heapq.heappush(cur, (cur_cost + next_cost, _next, next_cost))
    if len(already) == n:
        break
print(cost)

Fixed submission:
import sys
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
G = []
pos2id = {tuple(j) : i for i, j in enumerate(city_ls)}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
import heapq
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
sort_by_y = sorted([[j, i] for i,j in city_ls], key=lambda x:x[0])
G = []
id2pos = {i : tuple(j) for i, j in enumerate(city_ls)}
pos2id = {j : i for i, j in id2pos.items()}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Fixed submission:
import sys
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
G = []
pos2id = {tuple(j) : i for i, j in enumerate(city_ls)}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
import heapq
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
sort_by_y = sorted([[j, i] for i,j in city_ls], key=lambda x:x[0])
G = []
id2pos = {i : tuple(j) for i, j in enumerate(city_ls)}
pos2id = {j : i for i, j in id2pos.items()}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    current_by_y = sort_by_y[i].copy()
    current_by_y.reverse()
    if i < len(sort_by_x) - 1:
        next_by_x = sort_by_x[i + 1]
        next_by_y = sort_by_y[i + 1].copy()
        next_by_y.reverse()
    """
    if G.get(pos2id[current_by_x]) is None:
        G[pos2id[current_by_x]] = []
    if G.get(pos2id[current_by_y.reverse()]) is None:
        G[pos2id[current_by_y.reverse()]] = []
    """
    if i < len(sort_by_x) - 1:
        G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
        G.append((abs(current_by_y[1] - next_by_y[1]), current_by_y, next_by_y))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Fixed submission:
import sys
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
G = []
pos2id = {tuple(j) : i for i, j in enumerate(city_ls)}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
import heapq
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
sort_by_y = sorted([[j, i] for i,j in city_ls], key=lambda x:x[0])
G = []
id2pos = {i : tuple(j) for i, j in enumerate(city_ls)}
pos2id = {j : i for i, j in id2pos.items()}
for i in range(len(sort_by_x)):
    if i > 0:
        before_by_x = sort_by_x[i - 1]
        before_by_y = sort_by_y[i - 1].copy()
        before_by_y.reverse()
    current_by_x = sort_by_x[i]
    current_by_y = sort_by_y[i].copy()
    current_by_y.reverse()
    if i < len(sort_by_x) - 1:
        next_by_x = sort_by_x[i + 1]
        next_by_y = sort_by_y[i + 1].copy()
        next_by_y.reverse()
    """
    if G.get(pos2id[current_by_x]) is None:
        G[pos2id[current_by_x]] = []
    if G.get(pos2id[current_by_y.reverse()]) is None:
        G[pos2id[current_by_y.reverse()]] = []
    """
    if i < len(sort_by_x) - 1:
        G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
        G.append((abs(current_by_y[1] - next_by_y[1]), current_by_y, next_by_y))
    if i > 0:
        G.append((abs(current_by_x[0] - before_by_x[0]), current_by_x, before_by_x))
        G.append((abs(current_by_y[1] - before_by_y[1]), current_by_y, before_by_y))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Fixed submission:
import sys
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
G = []
pos2id = {tuple(j) : i for i, j in enumerate(city_ls)}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
import heapq
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
sort_by_y = sorted([[j, i] for i,j in city_ls], key=lambda x:x[0])
G = []
id2pos = {i : tuple(j) for i, j in enumerate(city_ls)}
pos2id = {j : i for i, j in id2pos.items()}
for i in range(len(sort_by_x)):
    if i > 0:
        before_by_x = sort_by_x[i - 1]
        before_by_y = sort_by_y[i - 1].copy()
        before_by_y.reverse()
    current_by_x = sort_by_x[i]
    current_by_y = sort_by_y[i].copy()
    current_by_y.reverse()
    if i < len(sort_by_x) - 1:
        next_by_x = sort_by_x[i + 1]
        next_by_y = sort_by_y[i + 1].copy()
        next_by_y.reverse()
    """
    if G.get(pos2id[current_by_x]) is None:
        G[pos2id[current_by_x]] = []
    if G.get(pos2id[current_by_y.reverse()]) is None:
        G[pos2id[current_by_y.reverse()]] = []
    """
    if i < len(sort_by_x) - 1:
        G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
        G.append((abs(current_by_y[1] - next_by_y[1]), current_by_y, next_by_y))
    if i > 0:
        G.append((abs(current_by_x[0] - before_by_x[0]), current_by_x, before_by_x))
        G.append((abs(current_by_y[1] - before_by_y[1]), current_by_y, before_by_y))
heapq.heapify(G)

sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

while len(G) > 0:
    (cost, _from, _to) = heapq.heappop(G)
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Fixed submission:
import sys
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
G = []
pos2id = {tuple(j) : i for i, j in enumerate(city_ls)}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import heapq
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in input().split()])
sort_by_x = sorted(city_ls, key=lambda x:x[0])
sort_by_y = sorted([[j,i] for i,j in city_ls], key=lambda x:x[0])
yet_ls = city_ls
cost = 0
current = city_ls[0]
yet_ls.remove(current)
candidate_set = set()
candidate_que = list()
while len(yet_ls) > 0:
    sort_by_x.remove(current)
    sort_by_y.remove([current[1], current[0]])
    point_by_x = bisect.bisect_left(sort_by_x, current)
    point_by_y = bisect.bisect_left(sort_by_y, [current[1], current[0]])
    if point_by_x == 0:
        x_candi = [sort_by_x[0]]
    elif point_by_x == len(sort_by_x):
        x_candi = [sort_by_x[-1]]
    else:
        x_candi = [sort_by_x[point_by_x - 1], sort_by_x[point_by_x]]
    if point_by_y == 0:
        y_candi = [sort_by_y[0]]
    elif point_by_y == len(sort_by_y):
        y_candi = [sort_by_y[-1]]
    else:
        y_candi = [sort_by_y[point_by_y - 1], sort_by_y[point_by_y]]
    y_candi = [[j,i] for i,j in y_candi]
    for candi in x_candi + y_candi:
        if candi not in yet_ls:
            continue
        hoge = [abs(current[0] - candi[0]), abs(current[1] - candi[1])]
        dis = min(hoge)
        heapq.heappush(candidate_que, [dis] + candi)
        candidate_set.add(tuple(candi))
    while True:
        best = heapq.heappop(candidate_que)
        if best[1:] in yet_ls:
            break
        if len(candidate_que) == 0:
            print("error")
            break
    candidate_set.remove(tuple(best[1:]))
    cost += best[0]
    current = best[1:]
    yet_ls.remove(current)
print(cost)

Fixed submission:
import sys
N = int(input())
city_ls = list()
for i in range(N):
    city_ls.append([int(i) for i in sys.stdin.readline().split()])
sort_by_x = sorted([[i, j] for i,j in city_ls], key=lambda x:x[0])
G = []
pos2id = {tuple(j) : i for i, j in enumerate(city_ls)}
for i in range(len(sort_by_x)-1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[0] - next_by_x[0]),current_by_x, next_by_x))
sort_by_x.sort(key=lambda x:x[1])
for i in range(len(sort_by_x) - 1):
    current_by_x = sort_by_x[i]
    next_by_x = sort_by_x[i + 1]
    G.append((abs(current_by_x[1] - next_by_x[1]), current_by_x, next_by_x))
G.sort(key=lambda x:x[0])
sum_cost = 0
par = [i for i in range(N)]
rank = [1 for i in range(N)]
def find(x):
    if par[x] == x:
        return x
    else:
        return find(par[x])

def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        pass
    else:
        if rank[x] > rank[y]:
            par[y] = x
            rank[x] += rank[y]
        else:
            par[x] = par[y]
            rank[y] += rank[x]

def is_same(x, y):
    if find(x) == find(y):
        return True
    else:
        return False

for i in G:
    (cost, _from, _to) = i
    _from = pos2id[tuple(_from)]
    _to = pos2id[tuple(_to)]
    if is_same(_from, _to):
        continue
    unite(_from, _to)
    sum_cost += cost
print(sum_cost)