Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop
from collections import defaultdict
n,m = map(int,input().split())
a = list(map(int,input().split()))
# edg = [[] for i in range(n)]
rank = [1] * n
par = [i for i in range(n)]
def root(x):
  if x == par[x]:
    return x
  par[x] = root(par[x])
  return par[x]
def union(a,b):
  a = root(a)
  b = root(b)
  if rank[a] < rank[b]:a,b = b,a
  if a == b: return False
  par[b] = a
  rank[a] += rank[b]
  return True
for _ in range(m):
  x,y = map(int, input().split())
  union(x,y)
d = defaultdict(list)
for i in range(n):
  ai = a[i]
  i = root(i)
  heappush(d[i],ai)

ans = 0
forest = []
for i in set(par):
  p = heappop(d[i])
  heappush(forest,(p,i))

for i in range(len(set(par))-1):  
  if len(forest) < 2:
    ans = "Impossible"
    break
  p1,i1 = heappop(forest)
  p2,i2 = heappop(forest)
  ans += p1 + p2
  union(i1,i2)
  if i1 == root(i1):
    for di2 in d[i2]:
      heappush(d[i1],di2)
  else:
    for di1 in d[i1]:
      heappush(d[i2],di1)
  i1 = root(i1)
  heappush(forest,(heappop(d[i1]),i1))

print(ans)


Fixed submission:
from heapq import heappush, heappop
from collections import defaultdict
n,m = map(int,input().split())
a = list(map(int,input().split()))
# edg = [[] for i in range(n)]
rank = [1] * n
par = [i for i in range(n)]
def root(x):
  if x == par[x]:
    return x
  par[x] = root(par[x])
  return par[x]
def union(a,b):
  a = root(a)
  b = root(b)
  if rank[a] < rank[b]:a,b = b,a
  if a == b: return False
  par[b] = a
  rank[a] += rank[b]
  return True
for _ in range(m):
  x,y = map(int, input().split())
  union(x,y)
d = defaultdict(list)
q = []
for i in range(n):
  ai = a[i]
  heappush(q,(ai,i))
  ri = root(i)
  heappush(d[ri],(ai,i))
if len(d.keys()) == 1:
  print(0)
else:
  ans = 0
  tmp = 0
  c = [0] * n

  for i in d.keys():
    tmp += 1
    t,p = heappop(d[i])
    c[p] = 1
    ans += t
  if 2*tmp-2 > n:
    print("Impossible")
  else:
    for _ in range(tmp-2):
      p,i = heappop(q)
      while c[i]:
        p,i = heappop(q)
      ans += p
    print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop
from collections import defauldict
n,m = map(int,input().split())
a = list(map(int,input().split()))
# edg = [[] for i in range(n)]
rank = [1] * n
par = [i for i in range(n)]
def root(x):
  if x == par[x]:
    return x
  par[x] = root(par[x])
  return par[x]
def union(a,b):
  a = root(a)
  b = root(b)
  if rank[a] < rank[b]:a,b = b,a
  if a == b: return False
  par[b] = a
  rank[a] += rank[b]
  return True
for _ in range(n):
  a,b = map(int,input().split())
  union(a,b)
d = defaultdict(list)
for i in range(n):
  ai = a[i]
  i = root(i)
  heappush(d[i],ai)

ans = 0
forest = []
for i in set(par):
  p = heappop(d[i])
  heappush(forest,(p,i))

for i in range(len(set(par))-1):
  p1,i1 = heappop(forest)
  p2,i2 = heappop(forest)
  ans += p1 + p2
  if d[i1]:
    heappush(forest,(heappop(d[i1]),i1))
  if d[i2]:
    heappush(forest,(heappop(d[i2]),i2))
  if len(forest) < 2:
    ans = "Impossible"
    break
print(ans)

Fixed submission:
from heapq import heappush, heappop
from collections import defaultdict
n,m = map(int,input().split())
a = list(map(int,input().split()))
# edg = [[] for i in range(n)]
rank = [1] * n
par = [i for i in range(n)]
def root(x):
  if x == par[x]:
    return x
  par[x] = root(par[x])
  return par[x]
def union(a,b):
  a = root(a)
  b = root(b)
  if rank[a] < rank[b]:a,b = b,a
  if a == b: return False
  par[b] = a
  rank[a] += rank[b]
  return True
for _ in range(m):
  x,y = map(int, input().split())
  union(x,y)
d = defaultdict(list)
q = []
for i in range(n):
  ai = a[i]
  heappush(q,(ai,i))
  ri = root(i)
  heappush(d[ri],(ai,i))
if len(d.keys()) == 1:
  print(0)
else:
  ans = 0
  tmp = 0
  c = [0] * n

  for i in d.keys():
    tmp += 1
    t,p = heappop(d[i])
    c[p] = 1
    ans += t
  if 2*tmp-2 > n:
    print("Impossible")
  else:
    for _ in range(tmp-2):
      p,i = heappop(q)
      while c[i]:
        p,i = heappop(q)
      ans += p
    print(ans)