#include <bits/stdc++.h>
using namespace std;
using long long int = long long int;
using pii = pair<int, int>;
using pll = pair<long long int, long long int>;
using vi = vector<int>;
using vl = vector<long long int>;
using vii = vector<pii>;
using vll = vector<pll>;
int N, M;
vector<vi> e;
vii d;
int main() {
  scanf("%d%d", &N, &M);
  d.resize(N);
  e.resize(N);
  for (long long int i = 0; i < N; i++)
    scanf("%d", &d[i].first), d[i].second = i;
  vi col(N, -1);
  vi co(N);
  vii exs(M);
  map<pii, int> ans;
  int u, v;
  for (long long int i = 0; i < M; i++) {
    scanf("%d%d", &u, &v);
    u--, v--;
    e[u].push_back(v);
    e[v].push_back(u);
    exs[i] = pii(u, v);
    ans[pii(u, v)] = 1e9;
    ans[pii(v, u)] = 1e9;
  }
  bool ok = true;
  sort(d.begin(), d.end());
  for (long long int i = 0; i < N; i++) {
    co[d[i].second] = i;
  }
  for (long long int i = 0; i < N; i++) {
    int dist = d[i].first;
    int from = d[i].second;
    if (col[from] != -1) continue;
    ok = false;
    for (int to : e[from]) {
      if (dist >= d[co[to]].first) {
        if (col[to] != -1)
          col[from] = col[to] ^ 1;
        else
          col[from] = 0, col[to] = 1;
        ans[pii(from, to)] = dist;
        ans[pii(to, from)] = dist;
        ok = true;
      }
    }
    if (!ok) {
      printf("%d\n", -1);
      return 0;
    }
  }
  for (long long int i = 0; i < N; i++) {
    if (col[i] == -1) {
      printf("%d\n", -1);
      return 0;
    }
  }
  for (long long int i = 0; i < N; i++) {
    printf("%c", col[i] ? 'B' : 'W');
  }
  printf("\n");
  for (long long int i = 0; i < M; i++) {
    printf("%d\n", ans[exs[i]]);
  }
  return 0;
}
