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')

        

            

