Buggy python submission (verdict: Runtime Error):
import sys
N, M = map(int, input().split())
D = list(zip(list(map(int, input().split())), list(range(N))))
G = [[] for _ in range(N)]
for i in range(N):
    u, v = map(int, input().split())
    G[u-1].append((v-1, i))
    G[v-1].append((u-1, i))
E = [e for e in D]
D.sort()
V = [-1]*N
j = 0
used = [False]*N
ans = [10**9]*M
BW = ['']*N
while j<N:
    d, v = D[j]
    for nv, i in G[v]:
        nd, k = E[nv]
        if used[nv] and d>nd:
            ans[i] = d-nd
            used[v] = True
            j+=1
            BW[v] = BW[nv]
            break
        if nd == d:
            used[v] = used[nv] = True
            ans[i] = d
            if BW[nv] == '':
                BW[nv] = 'B'
                BW[v] = 'W'
                j+=2
            elif BW[nv] == 'B':
                BW[v] = 'W'
                j+=1
            else:
                BW[v] = 'B'
                j+=1
            break
    if not used[v]:
        print(-1)
        sys.exit()
print(*BW, sep = '')
print(*ans, sep = '\n')

        

            



Fixed submission:
import sys
N, M = map(int, input().split())
D = list(map(int, input().split()))
DI = list(zip(D, list(range(N))))
G = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    G[u-1].append((v-1, i))
    G[v-1].append((u-1, i))
DI.sort()
V = [-1]*N
j = 0
used = [False]*N
ans = [10**9]*M
BW = ['']*N
for j in range(N):
    d, v = DI[j]
    if used[v]:
        continue
    for nv, i in G[v]:
        if d == D[nv]:
            if BW[nv] == '':
                BW[nv] = 'B'
                BW[v] = 'W'
                ans[i] = d
                used[v] = True
                used[nv] = True
            elif BW[nv] == 'W':
                BW[v] = 'B'
                ans[i] = d
                used[v] = True
            else:
                BW[v] = 'W'
                ans[i] = d
                used[v] = True
            break
        elif used[nv]:
            if BW[nv] == 'B':
                BW[v] = 'B'
                ans[i] = d-D[nv]
                used[v] = True
            else:
                BW[v] = 'W'
                ans[i] = d-D[nv]
                used[v] = True
            break
    if not used[v]:
        print(-1)
        sys.exit()
print(*BW, sep = '')
print(*ans, sep = '\n')

Buggy python submission (verdict: Runtime Error):
import sys
N, M = map(int, input().split())
D = list(zip(list(map(int, input().split())), list(range(N))))
G = [[] for _ in range(N)]
for i in range(N):
    u, v = map(int, input().split())
    G[u-1].append((v-1, i))
    G[v-1].append((u-1, i))
E = [e for e in D]
D.sort()
V = [-1]*N
j = 0
used = [False]*N
ans = [10**9]*M
BW = ['']*N
while j<N:
    d, v = D[j]
    for nv, i in G[v]:
        nd, k = E[nv]
        if used[nv]:
            ans[i] = d-nd
            used[v] = True
            j+=1
            BW[v] = BW[nv]
            break
        if nd == d:
            used[v] = used[nv] = True
            ans[i] = d
            BW[nv] = 'B'
            BW[v] = 'W'
            j+=2
            break
    if not used[v]:
        print(-1)
        sys.exit()
print(*BW, sep = '')
print(*ans, sep = '\n')

        

            



Fixed submission:
import sys
N, M = map(int, input().split())
D = list(map(int, input().split()))
DI = list(zip(D, list(range(N))))
G = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    G[u-1].append((v-1, i))
    G[v-1].append((u-1, i))
DI.sort()
V = [-1]*N
j = 0
used = [False]*N
ans = [10**9]*M
BW = ['']*N
for j in range(N):
    d, v = DI[j]
    if used[v]:
        continue
    for nv, i in G[v]:
        if d == D[nv]:
            if BW[nv] == '':
                BW[nv] = 'B'
                BW[v] = 'W'
                ans[i] = d
                used[v] = True
                used[nv] = True
            elif BW[nv] == 'W':
                BW[v] = 'B'
                ans[i] = d
                used[v] = True
            else:
                BW[v] = 'W'
                ans[i] = d
                used[v] = True
            break
        elif used[nv]:
            if BW[nv] == 'B':
                BW[v] = 'B'
                ans[i] = d-D[nv]
                used[v] = True
            else:
                BW[v] = 'W'
                ans[i] = d-D[nv]
                used[v] = True
            break
    if not used[v]:
        print(-1)
        sys.exit()
print(*BW, sep = '')
print(*ans, sep = '\n')