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

int ng() {
    cout << -1 << "\n";
    return 0;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m; cin >> n >> m;
    vector<int> d(n);
    vector<pair<int, int>> ds;
    for (int i = 0; i < n; i++) { 
        cin >> d[i];
        ds.emplace_back(d[i], i);
    }
    vector<pair<int, int>> e[n], uv;
    vector<int> c(m, 0), col(n, 0);
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        u--; v--;
        uv.emplace_back(u, v);
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
    }
    sort(ds.begin(), ds.end());
    for (int i = 0; i < n; i++) {
        auto &x = ds[i].second;
        auto &p = ds[i].first;
        bool ok = false;
        for (auto &ed: e[x]) {
            if (d[ed.first] <= p) {
                c[ed.second] = p;
                ok = true;
                break;
            }
        }
        if (!ok) return ng();
    }
    vector<int> f[n], visited(n, 0);
    for (int i = 0; i < m; i++) {
        if (c[i]) {
            auto &u = uv[i].first;
            auto &v = uv[i].second;
            f[u].emplace_back(v);
            f[v].emplace_back(u);
        } else c[i] = 1e9;
    }

    function<void(int, int)> dfs = [&](int curr, int prev) {
        visited[curr] = 1;
        for (auto &to: f[curr]) {
            if (to == prev) continue;
            col[to] = !col[curr];
            dfs(to, curr);
        }
    };

    for (int i = 0; i < n; i++) {
        if (!visited[i]) dfs(i, -1);
    }

    for (int i = 0; i < n; i++) {
        cout << (col[i] ? 'B' : 'W');
    }
    cout << "\n";
    for (int i = 0; i < m; i++) {
        cout << c[i] << "\n";
    }
    return 0;
}