import sys
from operator import itemgetter


def solve(n, m, ddd, links):
    ddi = list(enumerate(ddd))
    ddi.sort(key=itemgetter(1))
    LIMIT = 10 ** 9 + 1
    costs = [LIMIT] * m
    for v, dv in ddi:
        du, li, u = min(links[v])
        if du > dv:
            print(-1)
            return
        costs[li] = dv

    colors = [-1] * n
    for i in range(n):
        q = [(i, 0)]
        while q:
            v, color = q.pop()
            if colors[v] != -1:
                continue
            colors[v] = color
            for du, li, u in links[v]:
                if costs[li] == LIMIT:
                    continue
                q.append((u, color ^ 1))

    MAX = 10 ** 9
    costs = [MAX if cost == LIMIT else cost for cost in costs]

    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 = [[] for _ in range(n)]
for li, line in enumerate(sys.stdin):
    u, v = map(int, line.split())
    u -= 1
    v -= 1
    links[u].append((ddd[v], li, v))
    links[v].append((ddd[u], li, u))
solve(n, m, ddd, links)
