Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))

D_dic = {k: 0 for k in D}
T_dic = {k: 0 for k in T}

for i in range(N):
    if D_dic[D[i]]:
        a = D_dic[D[i]] + 1
        D_dic[D[i]] = a
    else:
        D_dic[D[i]] = 1
    
for j in range(M):
    if T_dic[T[j]]:
        b = T_dic[T[j]] + 1
        T_dic[T[j]] = b
    else:
        T_dic[T[j]] = 1

cou = 0
if N >= M:
    for k in range(M):
        if D_dic[T[k]]:
            c = D_dic[T[k]] - 1
            D_dic[T[k]] = c
            cou += 1
        else:
            x = 'NO'
            break
            
    if cou == M:
        x = 'YES'
else:
    x = 'NO'

print(x)

Fixed submission:
N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))

D_dic = {k: 0 for k in D}
T_dic = {k: 0 for k in T}

for i in range(N):
    d = D[i]
    if D_dic[d]:
        a = D_dic[d] + 1
        D_dic[d] = a
    else:
        D_dic[d] = 1
    
for j in range(M):
    t = T[j]
    if T_dic[t]:
        b = T_dic[t] + 1
        T_dic[t] = b
    else:
        T_dic[t] = 1

cou = 0
if N >= M:
    for k in range(M):
        tk = T[k]
        try:
            if D_dic[tk]:
                c = D_dic[tk] - 1
                D_dic[tk] = c
                cou += 1
            else:
                x = 'NO'
                break
        except KeyError:
            x = 'NO'
            break
            
    if cou == M:
        x = 'YES'
else:
    x = 'NO'

print(x)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))

cou = 0
if N >= M:
    for i in range(M):
        if T[i] in D:
            D.remove(T[i])
            cou += 1
        else:
            x = 'NO'
            break
            
    if cou == M:
        x = 'YES'
else:
    x = 'NO'

print(x)

Fixed submission:
N = int(input())
D = list(map(int, input().split()))
M = int(input())
T = list(map(int, input().split()))

D_dic = {k: 0 for k in D}
T_dic = {k: 0 for k in T}

for i in range(N):
    d = D[i]
    if D_dic[d]:
        a = D_dic[d] + 1
        D_dic[d] = a
    else:
        D_dic[d] = 1
    
for j in range(M):
    t = T[j]
    if T_dic[t]:
        b = T_dic[t] + 1
        T_dic[t] = b
    else:
        T_dic[t] = 1

cou = 0
if N >= M:
    for k in range(M):
        tk = T[k]
        try:
            if D_dic[tk]:
                c = D_dic[tk] - 1
                D_dic[tk] = c
                cou += 1
            else:
                x = 'NO'
                break
        except KeyError:
            x = 'NO'
            break
            
    if cou == M:
        x = 'YES'
else:
    x = 'NO'

print(x)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

lis = []
for i in range(N):
    a = int(input())
    lis.append(a)

num = 0
for n in set(lis):
    num += lis.count(n) % 2

print(num)


Fixed submission:
import collections

N = int(input())

def mod2(n):
    a = n % 2
    return a

lis = []
for i in range(N):
    a = int(input())
    lis.append(a)

count = collections.Counter(lis)

print(sum(map(mod2, count.values())))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

lis = []
for i in range(N):
    a = int(input())
    if a in lis:
        lis.remove(a)
    else:
        lis.append(a)
    
nsum = len(lis)
print(nsum)


Fixed submission:
import collections

N = int(input())

def mod2(n):
    a = n % 2
    return a

lis = []
for i in range(N):
    a = int(input())
    lis.append(a)

count = collections.Counter(lis)

print(sum(map(mod2, count.values())))
Buggy python submission (verdict: Runtime Error):
n = int(input())
L = [int(i) for i in input().split()]

L.sort()
ans = 0
for i in range(0, 2*N, 2):
    ans += min(L[i], L[i+1])

print(ans)

Fixed submission:
n = int(input())
L = [int(i) for i in input().split()]

L.sort()
ans = 0
for i in range(0, 2*n, 2):
    ans += min(L[i], L[i+1])

print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
if N%2==0 or N%3==0 or N%5==0 or N%7==0:
    print("Yes")
else:
    print("No")

Fixed submission:
A,B=map(int,input().split())
if 1<= A <= 9 and 1<= B <= 9:
    print(A*B)
else:
    print("-1")
Buggy python submission (verdict: Runtime Error):
N, K=map(int, input().split())
h=list(map(int, input().split()))
count=0

for i in range(N):
    if K <= h[i]:
        count += 1
print(count)


Fixed submission:
N = int(input())

if N%2 == 0:
    print((N/2)/N)
else:
    print(((N+1)/2)/N)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().sprit())

if a*b % 2 == 0:
    print("Even")
else:
    print("Odd")

Fixed submission:
a, b = map(int, input().split())

if a*b % 2 == 0:
    print("Even")
else:
    print("Odd")
Buggy python submission (verdict: Time Limit Exceeded):
def Q():
    N, A, B = map(int, input().split())
    MOD = 998244353

    if B == 0:
        print(1)
        exit()

    f = 1
    F = [1] * (N + 1)
    for i in range(1, N + 1):
        F[i] = f = f * i % MOD

    FI = [1] * (N + 1)
    FI[N] = fi = pow(f, MOD - 2, MOD)
    for i in range(N, 1, -1):
        FI[i - 1] = fi = fi * i % MOD

    z = 0
    for k in range(min(A + 1, N - B) if N - B - A else A + 1):
        x = (B - k) * F[B + k - 1] * FI[B] * FI[k] % MOD
        y = F[N + A - B - 2 * k - 1] * FI[N - B - k - 1] * FI[A - k] % MOD
        z = (z + x * y) % MOD

    print(z)

Q()

Fixed submission:
N, A, B = map(int, input().split())
MOD = 998244353

if B == 0:
    print(1)
    exit()

f = 1
F = [1] * (N + 1)
for i in range(1, N + 1):
    F[i] = f = f * i % MOD

FI = [1] * (N + 1)
FI[N] = fi = pow(f, MOD - 2, MOD)
for i in range(N, 1, -1):
    FI[i - 1] = fi = fi * i % MOD

z = 0
for k in range(min(A + 1, N - B) if N - B - A else A + 1):
    x = (B - k) * F[B + k - 1] * FI[B] * FI[k] % MOD
    y = F[N + A - B - 2 * k - 1] * FI[N - B - k - 1] * FI[A - k] % MOD
    z = (z + x * y) % MOD

print(z)

Buggy python submission (verdict: Time Limit Exceeded):
N, A, B = map(int, input().split())
MOD = 998244353

if B == 0:
    print(1)
    exit()

f = 1
F = [1] * (N + 1)
for i in range(1, N + 1):
    F[i] = f = f * i % MOD

FI = [1] * (N + 1)
FI[N] = fi = pow(f, MOD - 2, MOD)
for i in range(N, 1, -1):
    FI[i - 1] = fi = fi * i % MOD

z = 0
for k in range(min(A + 1, N - B) if N - B - A else A + 1):
    x = (B - k) * F[B + k - 1] * FI[B] * FI[k] % MOD
    y = F[N + A - B - 2 * k - 1] * FI[N - B - k - 1] * FI[A - k] % MOD
    z = (z + x * y) % MOD

print(z)

Fixed submission:
N, A, B = map(int, input().split())
MOD = 998244353

if B == 0:
    print(1)
    exit()

f = 1
F = [1] * (N + 1)
for i in range(1, N + 1):
    F[i] = f = f * i % MOD

FI = [1] * (N + 1)
FI[N] = fi = pow(f, MOD - 2, MOD)
for i in range(N, 1, -1):
    FI[i - 1] = fi = fi * i % MOD

z = 0
for k in range(min(A + 1, N - B) if N - B - A else A + 1):
    x = (B - k) * F[B + k - 1] * FI[B] * FI[k] % MOD
    y = F[N + A - B - 2 * k - 1] * FI[N - B - k - 1] * FI[A - k] % MOD
    z = (z + x * y) % MOD

print(z)
Buggy python submission (verdict: Time Limit Exceeded):
import fractions
from functools import reduce

n = int(input())
a = list(map(int, input().split()))

m1 = [0]
for i in a:
  m1.append(fractions.gcd(m1[-1], i))

m2 = [0]
for i in reversed(a):
  m2.append(fractions.gcd(m2[-1], i))

m = []
for i in range(len(a)):
  m.append(fractions.gcd(m1[i], m2[::-1][i + 1]))

print(max(m))

Fixed submission:
import fractions
from functools import reduce

n = int(input())
a = list(map(int, input().split()))

m1 = [0] * (n + 1)
m2 = [0] * (n + 1)
for i in range(1, n + 1):
  m1[i] = fractions.gcd(m1[i - 1], a[i - 1])
  m2[n - i] = fractions.gcd(m2[n - i + 1], a[n - i])

m = [0] * n
for i in range(n):
  m[i] = fractions.gcd(m1[i], m2[i + 1])

print(max(m))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()

s2 = s
for i in range(len(s)):
    s2 = s2.replace('10', '')
    s2 = s2.replace('01', '')
    if len(s) == len(s2):
        break

print(len(s) - len(s2))

Fixed submission:
s = input()

print(min(s.count('0'), s.count('1')) * 2)
Buggy python submission (verdict: Runtime Error):
def find(x):
  if P[x] < 0:
    return x
  else:
    return find(P[x])

N, M, *X = map(int,open(0).read().split())
P = [-1] * (N + 1)
S = N * (N - 1) // 2
r = [S]

while X:
  b, a = find(X.pop()), find(X.pop())
  if a != b:
    S -= P[a] * P[b]
    P[a] += P[b]
    P[b] = a
  r.append(S)

r.pop()
print(*reversed(r), sep='\n')

Fixed submission:
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(m)]

u = [-1] * (n + 1)

def find(v):
    if u[v] < 0:
        return v
    else:
        return find(u[v])

total = n * (n - 1) // 2
ans = [total]

for a, b in reversed(ab[1:]):
    af = find(a)
    bf = find(b)
    af, bf =sorted([af, bf])
    if af != bf:
        total -= u[af] * u[bf]
        u[af] += u[bf]
        u[bf] = af
    ans.append(total)

for a in reversed(ans):
    print(a)

Buggy python submission (verdict: Runtime Error):
def find(x):
  if P[x] < 0:
    return x
  else:
    return P[x]

N, M, *X = map(int,open(0).read().split())
P = [-1] * (N + 1)
S = N * (N - 1) // 2
r = [S]

while X:
  b, a = find(X.pop()), find(X.pop())
  if a != b:
    S -= P[a] * P[b]
    if P[a] > P[b]:
      a, b = b, a
    P[a] += P[b]
    P[b] = a
  r.append(S)

r.pop()
print(*reversed(r), sep='\n')

Fixed submission:
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(m)]

u = [-1] * (n + 1)

def find(v):
    if u[v] < 0:
        return v
    else:
        return find(u[v])

total = n * (n - 1) // 2
ans = [total]

for a, b in reversed(ab[1:]):
    af = find(a)
    bf = find(b)
    af, bf =sorted([af, bf])
    if af != bf:
        total -= u[af] * u[bf]
        u[af] += u[bf]
        u[bf] = af
    ans.append(total)

for a in reversed(ans):
    print(a)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(m)]

u = [-1] * (n + 1)

def find(v):
    if u[v] < 0:
        return v
    else:
        return find(u[v])

total = n * (n - 1) // 2
ans = [total]

for a, b in reversed(ab):
    af = find(a)
    bf = find(b)
    if af != bf:
        total -= u[af] * u[bf]
        u[af] += u[bf]
        u[bf] = af
    ans.append(total)

for a in reversed(ans[:-1]):
  print(a)

Fixed submission:
n, m = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(m)]

u = [-1] * (n + 1)

def find(v):
    if u[v] < 0:
        return v
    else:
        return find(u[v])

total = n * (n - 1) // 2
ans = [total]

for a, b in reversed(ab[1:]):
    af = find(a)
    bf = find(b)
    af, bf =sorted([af, bf])
    if af != bf:
        total -= u[af] * u[bf]
        u[af] += u[bf]
        u[bf] = af
    ans.append(total)

for a in reversed(ans):
    print(a)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

for i in range(M // N, 0, -1):
  if M % i == 0:
    print(i)
    exit()

Fixed submission:
N, M = map(int, input().split())

for i in range(M // N, 0, -1):
  if M % i == 0:
    print(i)
    exit()
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
T = input()

for i in range(len(S)):
    if S[i] != T[i]:
        S = S.translate(str.maketrans(S[i] + T[i], T[i] + S[i], ''))

if S == T:
  print('Yes')
else:
  print('No')

Fixed submission:
import collections

S = input()
T = input()

cs = collections.Counter(S)
ct = collections.Counter(T)

if sorted(ct.values()) == sorted(cs.values()):
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

c = 0
for l in range(n):
  for r in range(l, n):
    if sum(a[l:r + 1]) % m == 0:
      c += 1

print(c)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

d = {0: 1}
s = 0
for i in range(N):
  s += A[i]
  m = s % M
  if m in d:
    d[m] += 1
  else:
    d[m] = 1

ans = 0
for v in d.values():
  ans += v * (v - 1) // 2

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N, M = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(N)]

r = 0
for c in itertools.combinations(l, M):
  s = sum(abs(sum(v[i] for v in c)) for i in range(3))
  if s > r:
    r = s

print(r)

Fixed submission:
N, M = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(N)]

a = 0

for xs in (1, -1):
  for ys in (1, -1):
    for zs in (1, -1):
      l_s = [x*xs + y*ys + z*zs for x, y, z in l]
      l_s.sort(reverse=True)
      a = max(sum(l_s[:M]), a)

print(a)
Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
X = []
V = []
for _ in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

A = [0] * (N + 1)
A_max = [0] * (N + 1)
B = [0] * (N + 1)
B_max = [0] * (N + 1)
a_sum = 0
b_sum = 0
for i in range(N):
  a_sum += V[i]
  A[i + 1] = a_sum- X[i]
  A_max[i + 1] = max(A)
  b_sum += V[- i - 1]
  B[i + 1] = b_sum - (C - X[- i - 1])
  B_max[i + 1] = max(B)

X = [0] + X
V = [0] + V

m = max(0, A_max[-1], B_max[-1])
for i in range(1, N + 1):
  m = max(m, A[i] + B_max[N - i] - X[i], B[i] + A_max[N - i] - (C - X[N - i + 1]))

print(m)

Fixed submission:
N, C = map(int, input().split())
X = []
V = []
for _ in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

A = [0] * (N + 1)
A_max = [0] * (N + 1)
B = [0] * (N + 1)
B_max = [0] * (N + 1)
a_sum = 0
b_sum = 0
for i in range(N):
  a_sum += V[i]
  A[i + 1] = a_sum - X[i]
  A_max[i + 1] = max(A_max[i], A[i + 1])
  b_sum += V[- i - 1]
  B[i + 1] = b_sum - (C - X[- i - 1])
  B_max[i + 1] = max(B_max[i], B[i + 1])

X = [0] + X
V = [0] + V

m = max(0, A_max[-1], B_max[-1])
for i in range(1, N + 1):
  m = max(m, A[i] + B_max[N - i] - X[i], B[i] + A_max[N - i] - (C - X[N - i + 1]))

print(m)

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
X = []
V = []
for _ in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

A = [0] * (N + 1)
A_max = [0] * (N + 1)
B = [0] * (N + 1)
B_max = [0] * (N + 1)
for i in range(N):
  A[i + 1] = sum(V[:i + 1]) - X[i]
  A_max[i + 1] = max(A)
  B[i + 1] = sum(V[- i - 1:]) - (C - X[- i - 1])
  B_max[i + 1] = max(B)

X = [0] + X
V = [0] + V

m = max(0, A_max[-1], B_max[-1])
for i in range(1, N + 1):
  m = max(m, A[i] + B_max[N - i] - X[i], B[i] + A_max[N - i] - (C - X[N - i + 1]))

print(m)

Fixed submission:
N, C = map(int, input().split())
X = []
V = []
for _ in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

A = [0] * (N + 1)
A_max = [0] * (N + 1)
B = [0] * (N + 1)
B_max = [0] * (N + 1)
a_sum = 0
b_sum = 0
for i in range(N):
  a_sum += V[i]
  A[i + 1] = a_sum - X[i]
  A_max[i + 1] = max(A_max[i], A[i + 1])
  b_sum += V[- i - 1]
  B[i + 1] = b_sum - (C - X[- i - 1])
  B_max[i + 1] = max(B_max[i], B[i + 1])

X = [0] + X
V = [0] + V

m = max(0, A_max[-1], B_max[-1])
for i in range(1, N + 1):
  m = max(m, A[i] + B_max[N - i] - X[i], B[i] + A_max[N - i] - (C - X[N - i + 1]))

print(m)
Buggy python submission (verdict: Runtime Error):
n, h = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]

max_a = max(l, key=lambda x: x[0])[0]

l = [[a, b] for a, b in l if b > max_a]
l.sort(key=lambda x: x[1], reverse=True)
n = len(l)

c = 0
for i in range(n - 1):
  a, b = l[i]
  if a == max_a and l[i + 1][0] < max_a:
    l = l[i:]
    break
  h -= b
  c += 1
  if h <= 0:
    print(c)
    exit()
else:
  l = l[-1:]

last_a = l[0][0]
if l[0][1] >= h:
  print(c + 1)
else:
  for a, b in l[1:]:
    if b < last_a:
      break
    else:
      h -= b
      c += 1
      if h <= 0:
        print(c)
        exit()
  h -= l[0][1]
  c += 1
  print(c + h // last_a + (h % last_a != 0))

Fixed submission:
n, h = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]

max_a = max(l, key=lambda x: x[0])[0]
l = [[a, b] for a, b in l if b > max_a]
l.sort(key=lambda x: x[1], reverse=True)

c = 0
for a, b in l:
  h -= b
  c += 1
  if h <= 0:
    print(c)
    exit()

print(c + h // max_a + (h % max_a != 0))
Buggy python submission (verdict: Runtime Error):
N, C = map(int, input().split())
TV = [list(map(int, input().split())) for _ in range(N)]

TV = sorted([l for l in TV], key=lambda x: x[0])

X = []
for c in range(1, C + 1):
    TV_temp = [l for l in TV if l[2] == c]
    TV_T = list(zip(*TV_temp))
    l = []
    for tv in TV_temp:
        if tv[1] in TV_T[0]:
            i = TV_T[0].index(tv[1])
            l.append([tv[0], TV_T[1][i], c])
            TV_temp.pop(i)
        else:
            l.append(tv)
    
    X.extend(l)

TV = sorted([[l[0] - 0.5, l[1], l[2]] for l in X], key=lambda x: x[1])

c = 0
while TV:
    c += 1
    end = 0
    l_rem = []
    for i, tv in enumerate(TV):
        if tv[0] >= end:
            end = tv[1]
            l_rem.append(i)
    for i in reversed(l_rem):
        TV.pop(i)

print(c)

Fixed submission:
N, C = map(int, input().split())
TV = [list(map(int, input().split())) for _ in range(N)]

TV = sorted(TV, key=lambda x: x[1])

rec = [0] * (10 ** 5 + 1)
cs =  [set() for _ in range(10 ** 5 + 1)]

for s, t, c in TV:
    rec[t] -= 1
    if c not in cs[s]:
        rec[s - 1] += 1
    else:
        rec[s] += 1
    cs[t].add(c)

rec_s = [rec[0]]
for r in rec[1:]:
    rec_s.append(rec_s[-1] + r)

print(max(rec_s))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(N)]
X, Y = zip(*XY)
X_s = sorted(X)
Y_s = sorted(Y)

r = 10**20
for xi0 in range(N):
  for xi1 in range(xi0, N):
    for yi0 in range(N):
      for yi1 in range(yi0, N):
        x0 = X_s[xi0]
        x1 = X_s[xi1]
        y0 = Y_s[yi0]
        y1 = Y_s[yi1]
        c = 0
        for x, y in XY:
          if x0 <= x <=x1 and y0 <= y <= y1:
            c += 1
        if c >= K:
          r = min(r, (x1 - x0) * (y1 - y0))
print(r)

Fixed submission:
N, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(N)]
X, Y = zip(*XY)
X_s = sorted(X)
Y_s = sorted(Y)

r = 10**20
for xi0 in range(N):
  for xi1 in range(xi0, N):
    for yi0 in range(N):
      for yi1 in range(yi0, N):
        x0 = X_s[xi0]
        x1 = X_s[xi1]
        y0 = Y_s[yi0]
        y1 = Y_s[yi1]
        c = 0
        for x, y in XY:
          if x0 <= x <=x1 and y0 <= y <= y1:
            c += 1
        if c >= K:
          r = min(r, (x1 - x0) * (y1 - y0))
print(r)
Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix

N = int(input())
iXY = [[i] + list(map(int, input().split())) for i in range(N)]

P = []
for j in [1, 2]:
  iXY.sort(key=lambda a: a[j])
  for i in range(1, N):
    i0, x0, y0 =  iXY[i - 1]
    i1, x1, y1 =  iXY[i]
    P.append((min(abs(x1 - x0), abs(y1 - y0)), i0, i1))

d, i, j = zip(*P)
G = csr_matrix((d, (i, j)))
mst = minimum_spanning_tree(G)
print(int(mst.sum()))

Fixed submission:
import heapq

N = int(input())
iXY = [[i] + list(map(int, input().split())) for i in range(N)]
G = [set() for _ in range(N)]

iXY.sort(key=lambda a: a[1])
for i in range(1, N):
  i0, x0, y0 = iXY[i - 1]
  i1, x1, y1 = iXY[i]
  G[i0].add((i1))
  G[i1].add((i0))

iXY.sort(key=lambda a: a[2])
for i in range(1, N):
  i0, x0, y0 = iXY[i - 1]
  i1, x1, y1 = iXY[i]
  G[i0].add((i1))
  G[i1].add((i0))

Q = [(0, 0)]
B = set()
a = 0
iXY.sort()

while True:
  d, v = heapq.heappop(Q)
  while v in B:
     d, v = heapq.heappop(Q)
  a += d
  B.add(v)
  if len(B) == N:
    break
  for i in G[v]:
    if i not in B:
      heapq.heappush(Q, (min(abs(iXY[v][1] - iXY[i][1]), abs(iXY[v][2] - iXY[i][2])), i))
print(a)

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import lil_matrix

N = int(input())
iXY = [[i] + list(map(int, input().split())) for i in range(N)]

G = lil_matrix((N, N))

for j in [1, 2]:
  iXY.sort(key=lambda a: a[j])
  for i in range(1, N):
    i0, x0, y0 =  iXY[i - 1]
    i1, x1, y1 =  iXY[i]
    G[i0, i1] = min(abs(x1 - x0), abs(y1 - y0))

mst = minimum_spanning_tree(G)
print(int(mst.sum()))

Fixed submission:
import heapq

N = int(input())
iXY = [[i] + list(map(int, input().split())) for i in range(N)]
G = [set() for _ in range(N)]

iXY.sort(key=lambda a: a[1])
for i in range(1, N):
  i0, x0, y0 = iXY[i - 1]
  i1, x1, y1 = iXY[i]
  G[i0].add((i1))
  G[i1].add((i0))

iXY.sort(key=lambda a: a[2])
for i in range(1, N):
  i0, x0, y0 = iXY[i - 1]
  i1, x1, y1 = iXY[i]
  G[i0].add((i1))
  G[i1].add((i0))

Q = [(0, 0)]
B = set()
a = 0
iXY.sort()

while True:
  d, v = heapq.heappop(Q)
  while v in B:
     d, v = heapq.heappop(Q)
  a += d
  B.add(v)
  if len(B) == N:
    break
  for i in G[v]:
    if i not in B:
      heapq.heappush(Q, (min(abs(iXY[v][1] - iXY[i][1]), abs(iXY[v][2] - iXY[i][2])), i))
print(a)
Buggy python submission (verdict: Runtime Error):
N = int(input())
P = list(map(int, input().split()))

Pi = [0] * (N + 1)
for i, n in enumerate(P, 1):
    Pi[n] = i

T = [0] * N
f = [0] * N

if Pi[N] > Pi[N - 1]:
    T[N - 1] = 0
    f[N - 1] = N - 1
else:
    T[N - 1] = 1
    f[N - 1] = N

for i in range(N - 2, 0, -1):
    if T[i + 1] == 0:
        i_i = Pi[i]
        i_ii = Pi[i + 1]
        if i_ii > i_i:
            T[i] = T[i + 1]
            f[i] = f[i + 1]
        else:
            T[i] = T[i + 1] + 1
            f[i] = i + 1
    else:
        i_f = Pi[f[i + 1]]
        i_i = Pi[i]
        i_ii = Pi[i + 1]
        if i_f < i_i < i_ii or i_ii < i_f < i_i or i_i < i_ii < i_f:
            T[i] = T[i + 1]
            f[i] = f[i + 1]
        else:
            T[i] = T[i + 1] + 1
            f[i] = i + 1

print(T[1])

Fixed submission:
N = int(input())
P = list(map(int, input().split()))

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

Pi = [0] * (N + 1)
for i, n in enumerate(P, 1):
    Pi[n] = i

T = [0] * N
f = [0] * N

if Pi[N] > Pi[N - 1]:
    T[N - 1] = 0
    f[N - 1] = N - 1
else:
    T[N - 1] = 1
    f[N - 1] = N

for i in range(N - 2, 0, -1):
    if T[i + 1] == 0:
        i_i = Pi[i]
        i_ii = Pi[i + 1]
        if i_ii > i_i:
            T[i] = T[i + 1]
            f[i] = f[i + 1]
        else:
            T[i] = T[i + 1] + 1
            f[i] = i + 1
    else:
        i_f = Pi[f[i + 1]]
        i_i = Pi[i]
        i_ii = Pi[i + 1]
        if i_f < i_i < i_ii or i_ii < i_f < i_i or i_i < i_ii < i_f:
            T[i] = T[i + 1]
            f[i] = f[i + 1]
        else:
            T[i] = T[i + 1] + 1
            f[i] = i + 1

print(T[1])