import sys
from bisect import bisect_left
import sys
input=sys.stdin.readline

N,M=map(int,input().split())
D=[0]+list(map(int,input().split()))
UV=[]

que=[]

S=[-1]*(N+1)
C=[0]*M
edge=[[] for i in range(N+1)]

for i in range(M):
    U,V=map(int,input().split())
    if U>V:
        U,V=V,U
    UV.append([U,V,i])
    edge[U].append(V)
    edge[V].append(U)
    
    if D[U]==D[V]:
        
        que.append(U)
        que.append(V)
        C[i]=D[U]
        S[U]=1
        S[V]=0
        
UV.sort()
data=[]
for i in range(1,N+1):
    data.append([D[i],i])
data.sort()
if data[0][0]!=data[1][0]:
    print(-1)
    sys.exit()
def f(a,b):
    nnn=D[b]-D[a]
    if a>b:
        a,b=b,a
    b=bisect_left(UV,[a,b])
    C[UV[b][2]]=nnn
while que:
    h=[]
    for u in que:
        for v in edge[u]:
            if S[v]==-1 and D[v]>D[u]:
                f(u,v)
                S[v]=S[u]
                h.append(v)
    que=h
for i in range(M):
    if C[i]==0:
        C[i]=10**9
for i in range(1,N+1):
    if S[i]==1:
        S[i]="B"
    elif S[i]==0:
        S[i]="W"
    else:
        print(-1)
        sys.exit()
print("".join(S[1:]))
for u in C:
    print(u)