#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
long long rnd() {
  unsigned long long ans = 0;
  for (int i = (int)(0); i <= (int)(4); i++) ans = ans << 15 ^ rand();
  return ans % ((unsigned long long)1 << 63);
}
const int inf = 1e9, N = 100005, M = 400005;
int ne = 1, nxt[M], son[N], ed[M], di[M], d[N], q[N], ans[M >> 1], an[N];
bool cmp(int a, int b) { return d[a] < d[b]; }
void GG() {
  puts("-1");
  exit(0);
}
void ae(int a, int b) {
  nxt[++ne] = son[a];
  son[a] = ne;
  ed[ne] = b;
}
int main() {
  int n = read(), m = read();
  for (int i = (int)(1); i <= (int)(n); i++) d[q[i] = i] = read();
  for (int i = (int)(1); i <= (int)(m); i++) {
    int s = read(), t = read();
    ae(s, t);
    ae(t, s);
  }
  sort(q + 1, q + n + 1, cmp);
  for (int i = (int)(1); i <= (int)(m); i++) ans[i] = inf;
  memset(an, -1, sizeof(an));
  for (int o = (int)(1); o <= (int)(n); o++) {
    int i = q[o];
    if (an[i] != -1) continue;
    for (int j = son[i]; j; j = nxt[j]) {
      int t = ed[j];
      if (d[t] < d[i]) {
        an[i] = an[t];
        ans[j / 2] = d[i] - d[t];
      } else if (d[t] == d[i]) {
        if (an[t] == -1) an[t] = 0;
        an[i] = an[t] ^ 1;
        ans[j / 2] = d[i];
      }
    }
    if (an[i] == -1) GG();
  }
  for (int i = (int)(1); i <= (int)(n); i++)
    if (an[i])
      putchar('B');
    else
      putchar('W');
  puts("");
  for (int i = (int)(1); i <= (int)(m); i++) writeln(ans[i]);
}
