Problem p03440 (python)

Fixed submission:
from collections import*
from heapq import*
def main():
  n,m,*a=map(int,open(0).read().split())
  table=[-1]*n
  def root(x):
    while table[x]>=0:x=table[x]
    return x
  def unite(x,y):
    s1=root(x)
    s2=root(y)
    if s1!=s2:
      if table[s1]>table[s2]:s1,s2=s2,s1
      table[s1]+=table[s2]
      table[s2]=s1
  d=defaultdict(list)
  for x,y in zip(*[iter(a[n:])]*2):unite(x,y)
  for i,v in enumerate(a[:n]):d[root(i)]+=v,
  if len(d)<2:
    print(0)
    return
  *d,=map(sorted,d.values())
  c=sum(heappop(t)for t in d)
  l=[]
  for t in d:l+=t
  i=len(d)-2
  if len(l)<i:
    print('Impossible')
  else:
    print(c+sum(sorted(l)[:i]))
main()

Buggy submission (verdict: Runtime Error):
(n,m),a,*q=[map(int,t.split())for t in open(0)]
t=[-1]*n
def r(x):
 while-1<t[x]:x=t[x]
 return x
def u(x):
 x,y=map(r,x)
 if x!=y:
  if t[x]>t[y]:x,y=y,x
  t[x]+=t[y];t[y]=x
[*map(u,q)]
i=c=0
k=j=n+~m<<1
b,*d=eval('[],'*n)
for v in a:d[r(i)]+=v,;i+=1
for p in d:x,*y=sorted(p)+[0];c+=x;b+=y[:-1];j-=p>[]
print(k<1and'0'or(k>n)*'Impossible'or c+sum(sorted(b)[:j]))

Buggy submission (verdict: Runtime Error):
from collections import*
def main():
  n,m,*a=map(int,open(0).read().split())
  table=[-1]*n
  def root(x):
    while table[x]>=0:x=table[x]
    return x
  def unite(x,y):
    s1=root(x)
    s2=root(y)
    if s1!=s2:
      if table[s1]>table[s2]:s1,s2=s2,s1
      table[s1]+=table[s2]
      table[s2]=s1
  d=defaultdict(list)
  for x,y in zip(*[iter(a[n:])]*2):unite(x,y)
  for i,v in enumerate(a[:n]):d[root(i)]+=v,
  if len(d)<2:
    print(0)
    return
  *d,=map(sorted,d.values())
  c=sum(heappop(t)for t in d)
  l=[]
  for t in d:l+=t
  i=len(d)-2
  if len(l)<i:
    print('Impossible')
  else:
    print(c+sum(sorted(l)[:i]))
main()

Buggy submission (verdict: Runtime Error):
class UnionFind():
  def __init__(self,size):
    self.table=[-1]*size
  def root(self,x):
    while self.table[x]>=0:
      x=self.table[x]
    return x
  def unite(self,x,y):
    s1=self.root(x)
    s2=self.root(y)
    if s1!=s2:
      if self.table[s1]>self.table[s2]:
        s1,s2=s2,s1
      self.table[s1]+=self.table[s2]
      self.table[s2]=s1
  def same(self,x,y):
    return self.root(x)==self.root(y)
  def size(self,x):
    return -self.table[self.root(x)]
from collections import*
from heapq import*
def main():
  n,m,*a=map(int,open(0).read().split())
  u=UnionFind(n)
  d=defaultdict(list)
  for x,y in zip(*[iter(a[n:])]*2):u.unite(x,y)
  for i,v in enumerate(a[:n]):d[u.root(i)]+=v,
  *h,=map(sorted,d.values())
  heapify(h)
  p=heappop
  c=0
  while len(h)>1:
    x,y=p(h),p(h)
    if not x or not y:
      1/0
    c+=p(x)+p(y)
    heappush(h,list(merge(x,y)))
  print(c)
main()

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def main():
  n,m,*a=map(int,open(0).read().split())
  table=[-1]*n
  def root(x):
    while table[x]>=0:x=table[x]
    return x
  def unite(x,y):
    s1=root(x)
    s2=root(y)
    if s1!=s2:
      if table[s1]>table[s2]:s1,s2=s2,s1
      table[s1]+=table[s2]
      table[s2]=s1
  d=defaultdict(list)
  for x,y in zip(*[iter(a[n:])]*2):unite(x,y)
  for i,v in enumerate(a[:n]):d[root(i)]+=v,
  h=sorted(map(sorted,d.values()))
  p=heappop
  c=0
  while len(h)>1:
    x,y=p(h),p(h)
    if len(h)>2:
      if len(x)<2>len(y):
        t=[]
        while len(y)<2:
          t+=y,
          y=p(h)
        for u in t:heappush(h,u)
    if not x or not y:
      print('Impossible')
      exit()
    c+=p(x)+p(y)
    heappush(h,list(merge(x,y)))
  print(c)
main()