#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

bool dfs(const vector<vector<int64_t>>& edges, const vector<int64_t>& d,
         vector<int64_t>& colors, int64_t v) {
    for (const auto& u : edges[v]) {
        if (d[u] > d[v]) {
            continue;
        }

        colors[v] = 0;

        if (colors[u] < 0L) {
            if (!dfs(edges, d, colors, u)) {
                return false;
            }
        }

        colors[v] = 1 - colors[u];
    }

    return colors[v] >= 0;
}

int main() {
    int64_t n, m;
    cin >> n >> m;

    vector<int64_t> d(n);
    for (auto&& dd : d) {
        cin >> dd;
    }

    vector<vector<int64_t>> edges(n);
    vector<int64_t> weights(m), colors(n, -1L);
    for (auto i : irange(0L, m)) {
        int64_t u, v;
        cin >> u >> v;
        --u;
        --v;

        auto weight = max(d[u], d[v]);

        edges[u].push_back(v);
        edges[v].push_back(u);
        weights[i] = weight;
    }

    for (auto v : irange(0L, n)) {
        if (colors[v] < 0) {
            if (!dfs(edges, d, colors, v)) {
                cout << -1 << endl;
                return 0;
            }
        }
    }

    for (auto v : irange(0L, n)) {
        cout << (colors[v] ? "B" : "W");
    }
    cout << endl;

    for (auto w : weights) {
        cout << w << "\n";
    }
}