Problem p03440 (python)

Fixed submission:
from collections import defaultdict
from heapq import heappop, heappush
n,m = map(int, input().split())
a = list(map(int, input().split()))

uf = [-1]*n
def root(x):
    if uf[x] < 0:
        return x
    uf[x] = root(uf[x])
    return uf[x]
def unite(x,y):
    rx, ry = root(x), root(y)
    if rx == ry:
        return False
    if uf[rx] > uf[ry]:
        rx, ry = ry, rx
    uf[rx] += uf[ry]
    uf[ry] = rx
    return True

for i in range(m):
    x,y = map(int, input().split())
    unite(x,y)
if m == n-1:
    print(0)
elif (n-m-1)*2 > n:
    print("Impossible")
else:
    d = defaultdict(list)
    for i in range(n):
        heappush(d[root(i)], a[i])
    cnt = (n-m-1)*2
    ans = 0
    heap = []
    for value in d.values():
        ans += heappop(value)
        cnt -= 1
        while value:
            heappush(heap, heappop(value))
    for _ in range(cnt):
        ans += heappop(heap)
    print(ans)

Buggy submission (verdict: Runtime Error):
def LI(): return list(map(int, input().split()))
def LS(): return input().split()
def I(): return int(input())
def S(): return input()


n, m = LI()
a = LI()
d = dict([(i, [i])for i in range(n)])
#print(d)
for i in range(m):
    x, y = LI()
    d[x].append(y)
    d[y].append(x)

        
pow = [1]*n

def mori(num, mori_list):
    for c in d[num]:
        if pow[c] == 1:
            mori_list.append(a[c])
            pow[c] = 0
            mori(c, mori_list)

zen_mori = []
for i in range(n):
    if pow[i] == 1:
        mori_list = []
        mori(i, mori_list)
        zen_mori.append(sorted(mori_list)[::-1])


l = len(zen_mori)
check_list = [0]*l

if n - l < l - 2:
    print("Impossible")
    exit()
ans = 0
for cnt in range(l-1):
    pos = 0
    one, two = 0, 0
    use = []
    while True:
        min_list = [[zen_mori[i][-1] if len(zen_mori[i]) > 0 else (10**9 + 1), i] for i in range(l)]
        use = sorted(min_list)
        one, two = use[pos][1], use[pos+1][1]
        if check_list[one] == 1 and check_list[two] == 1:
            pos += 1
            continue
        if len(zen_mori[one]) == 1 and len(zen_mori[two]) == 1:
            pos += 1
            continue
        break
    ans += use[pos][0] + use[pos+1][0]
    try:
        zen_mori[one].pop()
    except:
        pass
    try:
        zen_mori[two].pop()
    except:
        pass
    check_list[one] = 1
    check_list[two] = 1
print(ans)

Buggy submission (verdict: Runtime Error):
    def LI(): return list(map(int, input().split()))
    def LS(): return input().split()
    def I(): return int(input())
    def S(): return input()
     
    n, m = LI()
    a = LI()
    d = dict([(i, [i])for i in range(n)])
    for i in range(m):
        x, y = LI()
        d[x].append(y)
        d[y].append(x)
     
            
    pow = [1]*n
     
    def mori(num, mori_list):
        for c in d[num]:
            if pow[c] == 1:
                mori_list.append(a[c])
                pow[c] = 0
                mori(c, mori_list)
     
    zen_mori = []
    for i in range(n):
        if pow[i] == 1:
            mori_list = []
            mori(i, mori_list)
            zen_mori.append(sorted(mori_list)[::-1])
     
     
    l = len(zen_mori)
    check_list = [0]*l
     
    if n - l < l - 2:
        print("Impossible")
        exit()
     
    ans = 0
    for cnt in range(l-1):
        pos = 0
        one, two = 0, 0
        use = []
        while True:
            min_list = [[zen_mori[i][-1] if len(zen_mori[i]) > 0 else (10**9 + 1), i] for i in range(l)]
            use = sorted(min_list)
            one, two = use[pos][1], use[pos+1][1]
            if check_list[one] == 1 and check_list[two] == 1:
                pos += 1
                continue
            if len(zen_mori[one]) == 1 and len(zen_mori[two]) == 1:
                pos += 1
                continue
            break
        ans += use[pos][0] + use[pos+1][0]
        try:
            zen_mori[one].pop()
        except:
            pass
        try:
            zen_mori[two].pop()
        except:
            pass
        check_list[one] = 1
        check_list[two] = 1
    print(ans)

Buggy submission (verdict: Runtime Error):
def LI(): return list(map(int, input().split()))
def LS(): return input().split()
def I(): return int(input())
def S(): return input()

n, m = LI()
a = LI()
d = dict([(i, [i])for i in range(n)])
for i in range(m):
    x, y = LI()
    d[x].append(y)
    d[y].append(x)

        
pow = [1]*n

def mori(num, mori_list):
    for c in d[num]:
        if pow[c] == 1:
            mori_list.append(a[c])
            pow[c] = 0
            mori(c, mori_list)

zen_mori = []
for i in range(n):
    if pow[i] == 1:
        mori_list = []
        mori(i, mori_list)
        zen_mori.append(sorted(mori_list)[::-1])


l = len(zen_mori)
check_list = [0]*l

if n - l < l - 2:
    print("Impossible")
    exit()

ans = 0
for cnt in range(l-1):
    pos = 0
    one, two = 0, 0
    use = []
    while True:
        min_list = [[zen_mori[i][-1] if len(zen_mori[i]) > 0 else (10**9 + 1), i] for i in range(l)]
        use = sorted(min_list)
        one, two = use[pos][1], use[pos+1][1]
        if check_list[one] == 1 and check_list[two] == 1:
            pos += 1
            continue
        if len(zen_mori[one]) == 1 and len(zen_mori[two]) == 1:
            pos += 1
            continue
        break
    ans += use[pos][0] + use[pos+1][0]
    check_list[one] = 1
    check_list[two] = 1
print(ans)