Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

b = [0] * 40 # ビットごとの1の個数

for i in a:
    bin_i = format(i, 'b')
    bin_i = bin_i[::-1]
    for j in range(len(bin_i)):
        if bin_i[j] == "1":
            b[j + 1] += 1

x = []
for i in b[1:]:
    if n - i <= i:
        x.append("0")
    elif n - i > i:
        x.append("1")
x = x[::-1]
i = 0
while int("".join(x), 2) > k:
    x[i] = "0"
    i += 1
x = int("".join(x), 2)

ans = 0

for i in a:
    ans += i ^ x

print(ans)

    

Fixed submission:
import sys
input = sys.stdin.readline

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

bin_k = format(k, 'b').zfill(50)

b = [0] * 50 # ビットごとの1の個数

for i in a:
    bin_i = format(i, 'b').zfill(50)
    for j in range(len(bin_i)):
        if bin_i[j] == "1":
            b[j] += 1

dp = [[0, 0] for _ in range(len(bin_k))]

if bin_k[0] == "0":
    dp[0][0] = b[0] * 2**(len(bin_k) - 1) # 0
else:
    dp[0][0] = (n - b[0]) * 2**(len(bin_k) - 1) # 1
    dp[0][1] = b[0] * 2**(len(bin_k) - 1) # 0

for i in range(1, len(bin_k)):
    if bin_k[i] == "0":
        dp[i][0] = dp[i - 1][0] + b[i] * 2**(len(bin_k) - (i + 1))
        if dp[i - 1][1] > 0:
            dp[i][1] = max(dp[i - 1][1] + b[i] * 2**(len(bin_k) - (i + 1)), dp[i - 1][1] + (n - b[i]) * 2**(len(bin_k) - (i + 1)))
    else:
        dp[i][0] = dp[i - 1][0] + (n - b[i]) * 2**(len(bin_i) - (i + 1))
        dp[i][1] = dp[i - 1][0] + b[i] * 2**(len(bin_k) - (i + 1))
        if dp[i - 1][1] > 0:
            dp[i][1] = max(dp[i][1], dp[i - 1][1] + (n - b[i]) * 2**(len(bin_i) - (i + 1)), dp[i - 1][1] + b[i] * 2**(len(bin_k) - (i + 1))) 

print(max(dp[-1][0], dp[-1][1]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n = int(input())
h = [int(x) for x in input().split()]

ans = 0

def solve(l, r):
    global ans
    if l >= r:
        return
    if 0 not in h[l:r]:
        minh = min(h[l:r])
        ans += minh
        for i in range(l, r):
            h[i] -= minh
        solve(l, r)
    else:
        for i in range(l, r):
            if h[i] == 0:
                solve(l, i)
                solve(i + 1, r)
solve(0, n)

print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
h = [int(x) for x in input().split()]
h = np.array(h,np.int64)
ans = 0
def split_(A):
    global ans
    if np.count_nonzero(A != 0) == 0:
        return
    else:
        minA = A.min()
        A -= minA
        ans += minA
        for i in range(len(A)):
            if A[i] == 0:
                split_(A[:i])
                split_(A[i+1:])
                break
split_(h)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*N
dp[1]=abs(h[1]-h[0])
for i in range(2,N):
    dp[i]=abs(h[i]-h[i-1])+dp[i-1]
    for j in range(i-2,max(0,i-K)-1,-1):
        dp2=abs(h[i]-h[j])+dp[j]
        if dp[i]>dp2:
            dp[i]=dp2
print(dp[N-1])

Fixed submission:
N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*N
dp[1]=abs(h[1]-h[0])
for i in range(2,N):
    dp[i]=abs(h[i]-h[i-1])+dp[i-1]
    for j in range(i-2,max(0,i-K)-1,-1):
        dp2=abs(h[i]-h[j])+dp[j]
        if dp[i]>dp2:
            dp[i]=dp2
print(dp[N-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, W = [int(x) for x in input().split()]
wv = []
for _ in range(N):
    wv.append([int(x) for x in input().split()])

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

for i in range(N):
    for j in range(W + 1):
        w, v = wv[i]
        if j >= w:
            dp[i + 1][j] = max(dp[i][j], dp[i][j - w] + v)
        else:
            dp[i + 1][j] = dp[i][j]

print(dp[N][W])

Fixed submission:
N,W=map(int,input().split())
A=[]
for _ in range(N):
    B=list(map(int,input().split()))
    A.append(B)
dp=[]
for _ in range(N):
    dp.append([0]*(W+1))
for j in range(W+1):
    if j>=A[0][0]:
        dp[0][j]=A[0][1]
for i in range(0,N-1):
    for w in range(W+1):
        if w>=A[i+1][0]:
            dp[i+1][w]=max(dp[i][w-A[i+1][0]]+A[i+1][1],dp[i][w])
        else:
            dp[i+1][w]=dp[i][w]
print(dp[N-1][W])
Buggy python submission (verdict: Runtime Error):
N,W=map(int,input().split())
A=[]
for _ in range(N):
    B=list(map(int,input().split()))
    A.append(B)
dp=[]
for _ in range(N):
    dp.append([0]*(W+1))
for j in range(W+1):
    if j>=A[0][0]:
        dp[0][j]=A[0][1]
for i in range(0,N-1):
    for w in range(W+1):
        if w>=A[i+1][0]:
            dp[i+1][w]=max(dp[i][w-A[i+1][0]]+A[i+1][1],dp[i][w])
        else:
            dp[i+1][w]=dp[i][w]
print(dp[N-1][W])

Fixed submission:
import sys
input = sys.stdin.readline

N, W = [int(x) for x in input().split()]
wv = []
for _ in range(N):
    wv.append([int(x) for x in input().split()])
V = 10 ** 5
inf = float("inf")

dp = [[inf] * (V + 1) for _ in range(N + 1)]
dp[0][0] = 0
ans = -1

for i in range(N):
    for j in range(V + 1):
        w, v = wv[i]
        if j >= v:
            dp[i + 1][j] = min(dp[i][j], dp[i][j - v] + w)
        else:
            dp[i + 1][j] = dp[i][j]
        if dp[i + 1][j] <= W:
            ans = max(ans, j)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,W=map(int,input().split())
A=[]
for _ in range(N):
    B=list(map(int,input().split()))
    A.append(B)
dp=[]
for _ in range(N):
    dp.append([0]*(W+1))
for j in range(W+1):
    if j>=A[0][0]:
        dp[0][j]=A[0][1]
for i in range(0,N-1):
    for w in range(W+1):
        if w>=A[i+1][0]:
            dp[i+1][w]=max(dp[i][w-A[i+1][0]]+A[i+1][1],dp[i][w])
        else:
            dp[i+1][w]=dp[i][w]
print(dp[N-1][W])


Fixed submission:
import sys
input = sys.stdin.readline

N, W = [int(x) for x in input().split()]
wv = []
for _ in range(N):
    wv.append([int(x) for x in input().split()])
V = 10 ** 5
inf = float("inf")

dp = [[inf] * (V + 1) for _ in range(N + 1)]
dp[0][0] = 0
ans = -1

for i in range(N):
    for j in range(V + 1):
        w, v = wv[i]
        if j >= v:
            dp[i + 1][j] = min(dp[i][j], dp[i][j - v] + w)
        else:
            dp[i + 1][j] = dp[i][j]
        if dp[i + 1][j] <= W:
            ans = max(ans, j)
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.readline
N = int(input())
B = [[int(x) for x in input().split()] for _ in range(N)]
def heighti(Cx,Cy,x,y,h):
    return abs(x-Cx) + abs(y-Cy) + h
ans = []
h0 = []
A = []
for l in B:
    if l[2] == 0:
        h0.append([l[0],l[1]])
    else:
        A.append(l)
for i in range(101):
    for j in range(101):
        tempH = heighti(i,j,A[0][0],A[0][1],A[0][2])
        if tempH < 0:
            break
        else:
            flag = 1
            for k in range(1,len(A)):
                if A[k][2] != 0 and heighti(i,j,A[k][0],A[k][1],A[k][2]) != tempH:
                    flag = 0
                    break
            if flag:
                ans.append([i,j,tempH])
if len(ans) == 1:
    print(*ans[0])
elif len(ans) == 0:
    for i in range(101):
        for j in range(101):
            for k in range(N):
                if heighti(i,j,A[k][0],A[k][1],A[k][2]) > 0:
                    print(*[i,j,heighti(i,j,A[k][0],A[k][1],A[k][2])])
                    sys.exit()
else:
    for m in ans:
        for n in h0:
            if heighti(n[0],n[1],m[0],m[1],m[2]) > 0:
                break
        print(*m)
        sys.exit()
        

Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())
B = [[int(x) for x in input().split()] for _ in range(N)]
def heighti(Cx,Cy,x,y,h):
    return abs(x-Cx) + abs(y-Cy) + h
def Heighti(Cx,Cy,x,y,h):
    return -abs(x-Cx) - abs(y-Cy) + h
ans = []
h0 = []
A = []
for l in B:
    if l[2] == 0:
        h0.append([l[0],l[1]])
    else:
        A.append(l)
if len(A) > 0:
    for i in range(101):
        for j in range(101):
            tempH = heighti(i,j,A[0][0],A[0][1],A[0][2])
            if tempH < 0:
                break
            else:
                flag = 1
                for k in range(1,len(A)):
                    if A[k][2] != 0 and heighti(i,j,A[k][0],A[k][1],A[k][2]) != tempH:
                        flag = 0
                        break
                if flag:
                    ans.append([i,j,tempH])
if len(ans) == 1:
    print(*ans[0])
elif len(ans) == 0:
    for i in range(101):
        for j in range(101):
            for k in range(N):
                if heighti(i,j,B[k][0],B[k][1],B[k][2]) > 0:
                    print(*[i,j,heighti(i,j,B[k][0],B[k][1],B[k][2])])
                    sys.exit()
else:
    for m in ans:
        flag = 1
        for n in h0:
            if Heighti(n[0],n[1],m[0],m[1],m[2]) > 0:
                flag = 0
                break
        if flag:
            print(*m)
            sys.exit()
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.readline
N = int(input())
ans = [111,222,333,444,555,666,777,888,999]
for i in ans:
    if ans >= N:
        print(ans)
        sys.exit()

Fixed submission:
import sys
import numpy as np
input = sys.stdin.readline
N = int(input())
ans = [111,222,333,444,555,666,777,888,999]
for i in ans:
    if i >= N:
        print(i)
        sys.exit()
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.readline
N, X = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
x -= X
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
    
x.tolist()
D = gcd(* x)
print(D)

Fixed submission:
import sys
import numpy as np
input = sys.stdin.readline
N, X = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if len(x) == 1:
    print(abs(X-x[0]))
    sys.exit()
x -= X
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
    
x.tolist()
D = gcd(* x)
print(abs(D))
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(xn[K-1])
if len(xp) >= K:
    ans.append(xp[K-1])

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-1] + abs(xn[-1]) + min(xp[-1],abs(xn[-1])))
print(min(ans))

Fixed submission:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
x = x[np.nonzero(x)]
if K == 0:
    print(0)
    sys.exit()
ans = []
for i in range(len(x)-K+1):
    if (x[i] < 0) != (x[i+K-1] < 0):
        ans.append(abs(x[i])+abs(x[i+K-1])+min(abs(x[i]),abs(x[i+K-1])))
    else:
        ans.append(max(abs(x[i]),abs(x[i+K-1])))
print(min(ans))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
S = list(input())
T = list(input())
N = len(S)
for i in range(N):
    S = S[-1] + S[:N-1]
    if S == T:
        print("Yes")
        sys.exit()
print("No")

Fixed submission:
import sys
input = sys.stdin.readline
S = list(input().rstrip())
T = list(input().rstrip())
N = len(S)
for i in range(N):
    S = list(S[-1]) + S[:N-1]
    if S == T:
        print("Yes")
        sys.exit()
print("No")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    else:
        islands[i + 1] = islands[i] | islands[i + 1]

ans += len(islands[n])

print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    elif len(islands[i]) > 0:
        islands[i + 1].add(min(islands[i]))

ans += len(islands[n])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    else:
        for j in islands[i]:
            islands[i + 1].add(j)

ans += len(islands[n])

print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    elif len(islands[i]) > 0:
        islands[i + 1].add(min(islands[i]))

ans += len(islands[n])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    else:
        for j in islands[i]:
            islands[i + 1].add(j)

ans += len(islands[n])

print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    elif len(islands[i]) > 0:
        islands[i + 1].add(min(islands[i]))

ans += len(islands[n])

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N = int(input())
N_str = list(str(N))
a = 0
for i in N_str:
    cnt += int(i)
if N % a == 0:
    print("Yes")
else:
    print("No")

Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())
N_str = list(str(N))
a = 0
for i in N_str:
    a += int(i)
if N % a == 0:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N = int(input())
N_str = list(str(N))
a = 0
for i in N_str:
    a += int(i)
if N % a == 0:
    print("Yes")
else:
    print("No")

Fixed submission:
import sys
input = sys.stdin.readline
A, B = [int(x) for x in input().split()]
if A <= 8 and B <= 8:
    print("Yay!")
else:
    print(":(")
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
D, N = [int(x) for x in input().split()]
ans = []
for i in range(1, 1010000):
    if i % (100 ** D) == 0 and i % (100 ** (D + 1)) != 0:
        ans.append(i)
print(ans[N-1])


Fixed submission:
import sys
input = sys.stdin.readline
D, N = [int(x) for x in input().split()]
ans = []
for i in range(1, 1010001):
    if i % (100 ** D) == 0 and i % (100 ** (D + 1)) != 0:
        ans.append(i)
print(ans[N-1])

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
D, N = [int(x) for x in input().split()]
ans = []
for i in range(1, 1010000):
    if i % (100 ** D) == 0 and i % (100 ** (D + 1)) != 0:
        ans.append(i)
print(ans[N-1])


Fixed submission:
import sys
input = sys.stdin.readline
D, N = [int(x) for x in input().split()]
ans = []
for i in range(1, 1010001):
    if i % (100 ** D) == 0 and i % (100 ** (D + 1)) != 0:
        ans.append(i)
print(ans[N-1])

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
D, N = [int(x) for x in input().split()]
ans = []
for i in range(1, 101000):
    if i % (100 ** D) == 0:
        ans.append(i)
print(ans[N-1])

Fixed submission:
import sys
input = sys.stdin.readline
D, N = [int(x) for x in input().split()]
ans = []
for i in range(1, 1010001):
    if i % (100 ** D) == 0 and i % (100 ** (D + 1)) != 0:
        ans.append(i)
print(ans[N-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]
xyz = [[int(x) for x in input().split()] for _ in range(n)]

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

for i in range(n):
    for j in range(1, i + 2):
        if i != 0:
            dp[i][j] = dp[i - 1][j]
            for k in range(i):
                x = dp[k][j - 1][0] + xyz[i][0]
                y = dp[k][j - 1][1] + xyz[i][1]
                z = dp[k][j - 1][2] + xyz[i][2]     
                if abs(x) + abs(y) + abs(z) >= abs(dp[i][j][0]) + abs(dp[i][j][1]) + abs(dp[i][j][2]):
                    dp[i][j] = [x, y, z]
        else:
            dp[0][1] = xyz[i]

a, b, c = dp[-1][m]

print(abs(a) + abs(b) + abs(c))

Fixed submission:
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]
xyz = [[int(x) for x in input().split()] for _ in range(n)]

ans = 0

for i in range(8):
    i_bin = bin(i)[2:].zfill(3)
    new_xyz_sum = []
    for j in range(n):
        tmp = 0
        x, y, z = xyz[j]
        if i_bin[0] == "0":
            x *= -1
        if i_bin[1] == "0":
            y *= -1
        if i_bin[2] == "0":
            z *= -1
        tmp = x + y + z
        new_xyz_sum.append(tmp)
    new_xyz_sum = sorted(new_xyz_sum, reverse=True)
    ans = max(ans, sum(new_xyz_sum[:m]))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
S = []
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, i+K+1):
        if s[i:j] not in S:
            S.append(s[i:j])
S.sort()
print("".join(S[K-1]))


Fixed submission:
import sys
input = sys.stdin.readline
S = set()
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, i+K+1):
        if "".join(s[i:j]) not in S:
            S.add("".join(s[i:j]))
S = list(S)
S.sort()
print("".join(S[K-1]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
S = set()
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        if "".join(s[i:j]) not in S:
            S.add("".join(s[i:j]))
S = list(S)
S.sort()
print("".join(S[K-1]))


Fixed submission:
import sys
input = sys.stdin.readline
S = set()
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, i+K+1):
        if "".join(s[i:j]) not in S:
            S.add("".join(s[i:j]))
S = list(S)
S.sort()
print("".join(S[K-1]))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
S = []
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, i+5):
        if s[i:j] not in S:
            S.append(s[i:j])
S.sort()
print("".join(S[K-1]))


Fixed submission:
import sys
input = sys.stdin.readline
S = set()
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, i+K+1):
        if "".join(s[i:j]) not in S:
            S.add("".join(s[i:j]))
S = list(S)
S.sort()
print("".join(S[K-1]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
S = []
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        if s[i:j] not in S:
            S.append(s[i:j])
S.sort()
print("".join(S[K-1]))


Fixed submission:
import sys
input = sys.stdin.readline
S = set()
s = list(input().rstrip())
K = int(input())
for i in range(len(s)):
    for j in range(i+1, i+K+1):
        if "".join(s[i:j]) not in S:
            S.add("".join(s[i:j]))
S = list(S)
S.sort()
print("".join(S[K-1]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]

from collections import deque

def bfs(u):
    d = [-1] * n
    queue = deque([u])
    d[u] = 0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] < 0:
                d[i] = d[v] + 1
                queue.append(i)
    return d

g = [[] for _ in range(n)]

for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
    g[y - 1].append(x - 1)

ans = 0

for i, j in enumerate(p):
    # print(i, j)
    d = bfs(i)
    # print(d)
    if d[j - 1] >= 0:
        ans += 1

print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]

#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)

    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find(x) == find(y)

#xが属する集合の個数
def size(x):
    return -par[find(x)]

#初期化
#根なら-size,子なら親の頂点
par = [-1]*n

###具体例###
for _ in range(m):
    a, b = [int(x) for x in input().split()]
    unite(a - 1, b - 1)

ans = 0

for i, j in enumerate(p):
    if same(i, j - 1):
        ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
X = [int(x) for x in input().split()]
for i in range(N):
    temp = X[:i] + X[i+1:]
    print(int(np.median(temp)))

Fixed submission:
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
X = np.array([int(x) for x in input().split()])
Y = np.sort(X)
right = N // 2
left = right - 1
right, left = Y[right], Y[left]
for i in range(N):
    if X[i] <= left:
        print(right)
    else:
        print(left)