import sys
from operator import itemgetter


def solve(n, m, ddd, links):
    ddi = list(enumerate(ddd))
    ddi.sort(key=itemgetter(1))
    colors = [-1] * n
    costs = [10 ** 9] * m
    for v, d in ddi:
        for li, u in links[v]:
            if ddd[u] > d:
                continue
            if colors[u] != -1:
                colors[v] = 0
                colors[u] = 1
            else:
                colors[v] = colors[u] ^ 1
            costs[li] = d
            break
        else:
            print(-1)
            return
    print(''.join('BW'[c] for c in colors))
    print('\n'.join(map(str, costs)))


n, m = map(int, input().split())
ddd = list(map(int, input().split()))
links = [set() for _ in range(n)]
for li, line in enumerate(sys.stdin):
    u, v = map(int, line.split())
    u -= 1
    v -= 1
    links[u].add((li, v))
    links[v].add((li, u))
solve(n, m, ddd, links)
