#include <bits/stdc++.h>
#define R register
#define mp make_pair
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 110000, M = N << 1, inf = 1e9;

int n, m, d[N], nxt[M], to[M], noedg = 1, hd[N], val[N], tag[N];
priority_queue<pii> que;
char s[N];

struct DSU {
//
int fa[M];

inline void init() {
    for (R int i = 1; i <= n * 2; ++i) fa[i] = i;
    return;
}

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

inline void unite(int x, int y) {
    fa[find(x)] = find(y);
    return;
}
//
} dsu;

inline void addEdg(int x, int y) {
    nxt[++noedg] = hd[x], hd[x] = noedg, to[noedg] = y;
    nxt[++noedg] = hd[y], hd[y] = noedg, to[noedg] = x;
    return;
}

template <class T>
inline void read(T &x) {
    x = 0;
    char ch = getchar(), w = 0;
    while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
    while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    x = w ? -x : x;
    return;
}

inline char rev(char ch) {
    return ch == 'B' ? 'W' : 'B';
}

int main() {
    int x, y;
    read(n), read(m);
    for (R int i = 1; i <= n; ++i) read(d[i]), que.push(mp(d[i], i));
    for (R int i = 1; i <= m; ++i) read(x), read(y), addEdg(x, y);
    dsu.init();
    while (que.size()) {
        int now = que.top().second, pos = now; 
        que.pop();
        if (tag[now]) {
            for (R int i = hd[now]; i; i = nxt[i])
                if (!val[i >> 1]) val[i >> 1] = inf;
            continue;
        }
        for (R int i = hd[now], v; i; i = nxt[i])
            v = to[i], pos = d[v] <= d[pos] ? v : pos;
        if (pos == now) return printf("-1\n"), 0;
        if (d[pos] < d[now]) {
            dsu.unite(pos, now), dsu.unite(pos + n, now + n);
            for (R int i = hd[now], v; i; i = nxt[i]) {
                if ((v = to[i]) == pos)
                    val[i >> 1] = d[now] - d[v];
                else if (!val[i >> 1])
                    val[i >> 1] = inf;
            }
        }
        else {
            dsu.unite(pos + n, now), dsu.unite(pos, now + n);
            tag[pos] = 1;
            for (R int i = hd[now], v; i; i = nxt[i]) {
                if ((v = to[i]) == pos)
                    val[i >> 1] = d[now];
                else if (!val[i >> 1])
                    val[i >> 1] = inf;
            }
        }
    }
    for (R int i = 1; i <= n; ++i) {
        if (s[i]) continue;
        int k = dsu.find(i);
        if (k > n) {
            if (!s[k - n]) s[k - n] = 'W';
            s[i] = rev(s[k - n]);
        }
        else {
            if (!s[k]) s[k] = 'W';
            s[i] = s[k];
        }
    }
    printf("%s\n", s + 1);
    for (R int i = 1; i <= m; ++i) printf("%d\n", val[i]);
    return 0;
}