#include <bits/stdc++.h>

using namespace std;

const int max_n = 200222, inf = 1000000000;

int n, m, d[max_n], u[max_n], v[max_n];
int w[max_n], p[max_n];
vector<int> g[max_n];
int col[max_n];

bool cmp(int a, int b) {
    return d[a] < d[b];
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &d[i]);
        p[i] = i;
    }
    for (int i = 0; i < m; ++i) {
        scanf("%d%d", &u[i], &v[i]);
        --u[i];
        --v[i];
        g[u[i]].push_back(i);
        g[v[i]].push_back(i);
        w[i] = inf;
    }
    sort(p, p + n, cmp);
    for (int I = 0; I < n; ++I) {
        int i = p[I];
        if (col[i]) {
            continue;
        }
        for (int num : g[i]) {
            const int to = i ^ u[num] ^ v[num];
            if (d[to] <= d[i]) {
                if (col[to]) {
                    col[i] = 3 - col[to];
                    w[num] = d[i];
                } else {
                    col[i] = 1;
                    col[to] = 2;
                    w[num] = d[i];
                }
                break;
            }
        }
        if (!col[i]) {
            puts("-1");
            return 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        printf("%c", " WB"[col[i]]);
    }
    puts("");
    for (int i = 0; i < m; ++i) {
        printf("%d\n", w[i]);
    }
    return 0;
}
