#include <cstdio>
#include <queue>
#include <functional>
#include <vector>
using namespace std;
int main() {
    int n, m; scanf(" %d %d", &n, &m);
    int d[n]; for (int i = 0; i < n; i++) scanf(" %d", &d[i]);
    vector<vector<pair<int, int> > > adj(n);
    for (int i = 0; i < m; i++) {
        int u, v; scanf(" %d %d", &u, &v);
        u--; v--;
        adj[u].push_back({v, i});
        adj[v].push_back({u, i});
    }
    const int WMAX = 1e9;
    int w[m], col[n];
    fill(col, col+n, -1);
    fill(w, w+m, WMAX);

    vector<bool> root(n, false);
    for (int i = 0; i < n; i++) {
        bool eq = false, down = false;
        for (auto e : adj[i]) {
            const int j = e.first;
            if (d[j] < d[i]) { down = true; break; }
            else if (d[j] == d[i]) eq = true;
        }
        if (!down) {
            if (!eq) { puts("-1"); return 0; }
            else root[i] = true;
        }
    }
    function <void(int, int)> process_roots = [&](int i, int c) {
        col[i] = c;
        root[i] = true;
        for (auto e : adj[i]) {
            int j = e.first, k = e.second;
            if (d[j] == d[i] && col[j] == -1) {
                w[k] = d[i];
                process_roots(j, 1^c);
            }
        }
    };
    for (int i = 0; i < n; i++) if (root[i] && col[i] == -1) process_roots(i, 0);

    function <void(int, int)> process_tree = [&](int i, int c) {
        for (auto e : adj[i]) {
            const int j = e.first, k = e.second;
            if (d[j] > d[i] && col[j] == -1) {
                w[k] = col[i] == c ? d[j] - d[i] : d[j];
                col[j] = c;
                process_tree(j, c);
            }
        }
    };
    for (int i = 0; i < n; i++) if (root[i]) process_tree(i, 1^col[i]);

    for (int i = 0; i < n; i++) putchar(col[i] == 0 ? 'B' : 'W');
    putchar('\n');
    for (int i = 0; i < m; i++) printf("%d\n", w[i]);
}
