#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <tuple>
#include <cstring>
#include <map>
#include <iomanip>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tlll tuple<ll, ll, ll>

vector<int> G[200010];
map<Pii, int> edge;
int d[200010];
int ans[200020] = {};
bool col[200010] = {};
bool vis[200010] = {};

int par[200010];
int rnk[200010];

void init(int n) {
    for (int i = 0; i < n; i++) {
        par[i] = i;
        rnk[i] = 0;
    }
}

int find(int x) {
    if (par[x] == x) return x;
    else return par[x] = find(par[x]);
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) par[x] = y;
    else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

bool same(int x, int y) {
    return find(x) == find(y);
}

void dfs(int k) {
    vis[k] = 1;
    for (auto & i : G[k]) {
        if (!vis[i]) {
            ans[edge[Pii(min(i, k), max(i, k))]] = d[i];
            col[i] = (1 ^ col[k]);
            dfs(i);
        }
    }
}

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

    vector<Pii> vet;
    for (int i = 0; i < n; i++) {
        cin >> d[i];
        vet.emplace_back(d[i], i);
    }
    sort(all(vet));

    int u[m + 5], v[m + 5];
    for (int i = 0; i < m; i++) {
        cin >> u[i] >> v[i];
        u[i]--,  v[i]--;
        if (u[i] > v[i]) swap(u[i], v[i]);
        //G[u[i]].push_back(v[i]);
        //G[v[i]].push_back(u[i]);
        edge[Pii(u[i], v[i])] = i;
    }

    int mn_idx = 0;
    bool mns[n + 5] = {};
    while (vet[0].first == vet[mn_idx].first) {
        mns[vet[mn_idx].second] = 1;
        mn_idx++;
    }
    cerr << mn_idx << endl;

    int ok = -1;
    for (int i = 0; i < m; i++) {
        if (mns[u[i]] && mns[v[i]]) {
            //cerr << u[i] _ v[i] << endl;
            ok = i;  break;
        }
    }
    cerr << ok << endl;

    if (ok == -1) {
        cout << -1 << endl;
        return 0;
    }

    init(n);
    unite(u[ok], v[ok]);
    vector<int> tree = {ok};
    ans[ok] = vet[0].first;
    col[u[ok]] = 0;
    col[v[ok]] = 1;
    G[u[ok]].push_back(v[ok]);
    G[v[ok]].push_back(u[ok]);
    for (int i = 0; i < m; i++) {
        if (!same(u[i], v[i])) {
            tree.push_back(i);
            unite(u[i], v[i]);
            G[u[i]].push_back(v[i]);
            G[v[i]].push_back(u[i]);
        }
    }
    sort(all(tree));
    cerr << "tree: ";
    for (auto & i : tree) cerr << i << " ";
    cerr << endl;

    vis[v[ok]] = 1;
    dfs(u[ok]);
    vis[v[ok]] = 0;
    dfs(v[ok]);

    for (int i = 0; i < m; i++) {
        if (ans[i] == 0) ans[i] = 1e9;
    }

    for (int i = 0; i < n; i++) {
        cout << (col[i] ? 'B' : 'W');
    }
    cout << endl;
    for (int i = 0; i < m; i++) cout << ans[i] << endl;





}
