#include <bits/stdc++.h>
using namespace std;

const int kN = 200'000 + 5;
const int kInf = 1'000'000'000;
vector<int> g[kN];
int d[kN], ed[kN], w[kN];
bool used[kN];
char ans[kN];

int main() {
    int n, m; scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) scanf("%d", &d[i]);
    for (int i = 0; i < m; ++i) {
        int u, v; scanf("%d%d", &u, &v);
        u--, v--;
        ed[i] = u ^ v;
        g[u].push_back(i);
        g[v].push_back(i);
        w[i] = kInf;
    }
    set<pair<int, int>> s;
    for (int i = 0; i < n; ++i) s.insert(make_pair(d[i], i));
    while (!s.empty()) {
        int r = s.begin()->second;
        s.erase(s.begin());
        bool ok = false;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        ans[r] = 'B';
        used[r] = true;
        for (int e : g[r]) {
            int u = ed[e] ^ r; 
            if (used[u]) continue;
            used[u] = true;
            ok |= d[u] == d[r];
            w[e] = d[u];
            pq.emplace(d[u], u);
            ans[u] = 'W';
        }
        if (!ok) {
            puts("-1");
            return 0;
        }
        while (!pq.empty()) {
            int x = pq.top().second;
            pq.pop();
            s.erase(make_pair(d[x], x));
            for (int e : g[x]) {
                int u = ed[e] ^ x;
                if (used[u]) continue;
                if (d[u] <= d[x]) continue;
                used[u] = true;
                w[e] = d[u] - d[x];
                ans[u] = 'W';
                pq.emplace(d[u], u);
            }
        }
    }
    printf("%s\n", ans);
    for (int i = 0; i < m; ++i) printf("%d\n", w[i]);
}
