#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <functional>
#include <iomanip>
#include <numeric>

using namespace std;

const int N = 2e5 + 5;

int d[N];
vector<pair<int, int>> g[N];
int color[N];
int weight[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> d[i];
    }
    for (int i = 0; i < m; i++) {
        int v, u;
        cin >> v >> u;
        --v;
        --u;
        g[v].emplace_back(u, i);
        g[u].emplace_back(v, i);
    }
    vector<int> kek(n);
    iota(kek.begin(), kek.end(), 0);
    sort(kek.begin(), kek.end(), [&](int a, int b) {
        return d[a] < d[b];
    });
    fill(weight, weight + m, 1e9);
    for (int i = 0; i < n; i++) {
        int v = kek[i];
        bool f = false;
        for (auto e : g[v]) {
            if ((color[v] != color[e.first] || color[v] * color[e.first] == 0) && d[e.first] <= d[v]) {
                if (color[v]) {
                    color[e.first] = 3 - color[v];
                }
                if (color[e.first]) {
                    color[v] = 3 - color[e.first];
                }
                if (!color[v]) {
                    color[v] = 1;
                    color[e.first] = 2;
                }
                weight[e.second] = d[v];
                f = true;
                break;
            }
        }
        if (!f) {
            cout << -1 << endl;
            return 0;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << "BW"[color[i] == 1];
    }
    cout << '\n';
    for (int i = 0; i < m; i++) {
        cout << weight[i] << '\n';
    }
    return 0;
}