#include <bits/stdc++.h>
using namespace std;
template <typename T>
void fin(T a) {
  cout << a << endl;
  exit(0);
}
const int INF = 1e9;
template <typename T>
void chmin(T &a, T b) {
  if (a > b) a = b;
}
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> d(n), u(m), v(m);
  for (int i = 0; i < n; i++) cin >> d[i];
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
  }
  vector<int> edge[n];
  for (int i = 0; i < m; i++) {
    edge[u[i]].push_back(v[i]);
    edge[v[i]].push_back(u[i]);
  }
  for (int i = 0; i < n; i++) {
    int A = INF;
    for (int p : edge[i]) chmin(A, d[p]);
    if (A > d[i]) fin(-1);
  }
  set<pair<int, int> > s;
  vector<int> edge2[n];
  map<pair<int, int>, int> ma;
  for (int i = 0; i < n; i++) {
    int idx, D = INF;
    for (int p : edge[i])
      if (D > d[p]) D = d[p], idx = p;
    int A = idx, B = i;
    if (A > B) swap(A, B);
    if (s.count(pair<int, int>(A, B))) continue;
    s.insert(pair<int, int>(A, B));
    edge2[A].push_back(B);
    edge2[B].push_back(A);
    ma[pair<int, int>(A, B)] = d[i];
  }
  vector<int> col(n, 0);
  queue<int> que;
  for (int i = 0; i < n; i++) {
    if (col[i]) continue;
    col[i] = 1;
    que.push(i);
    while (que.size()) {
      int p = que.front();
      que.pop();
      for (int q : edge2[p]) {
        if (col[q]) continue;
        col[q] = -col[p];
        que.push(q);
      }
    }
  }
  for (int i = 0; i < n; i++) cout << (~col[i] ? 'B' : 'W');
  cout << endl;
  for (int i = 0; i < m; i++) {
    if (u[i] > v[i]) swap(u[i], v[i]);
    cout << (ma[pair<int, int>(u[i], v[i])] ? ma[pair<int, int>(u[i], v[i])]
                                            : INF)
         << endl;
  }
}
