#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using P = pair<int, int>;
int C[101010];
int D[101010];
int E[202020];
int d[101010];
void bfs(vvi &G, int s, int *d) {
  queue<int> q;
  d[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto &e : G[u]) {
      if (d[e] > 0) continue;
      if (d[e] == 0 && e != s) d[e] = d[u] + 1, q.push(e);
    }
  }
  return;
}
int main() {
  int N, M, first, second;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i += 1) scanf("%d", &D[i]);
  vvi G(N);
  map<P, int> m;
  for (int i = 0; i < M; i += 1) {
    scanf("%d %d", &first, &second);
    first--, second--;
    if (first > second) swap(first, second);
    G[first].push_back(second);
    G[second].push_back(first);
    m[{first, second}] = i;
  }
  bool ok = true;
  vvi G2(N);
  for (int v = 0; v < N; v += 1) {
    priority_queue<P, vector<P>, greater<P>> pq;
    int c = 0;
    for (auto &u : G[v]) {
      if (D[v] < D[u]) c++;
      pq.push({D[u], u});
    }
    if (c == G[v].size()) {
      ok = false;
      break;
    }
    if (!pq.empty()) {
      int u = pq.top().second;
      G2[u].push_back(v);
      G2[v].push_back(u);
    }
  }
  bfs(G2, 0, d);
  for (int v = 0; v < N; v += 1) {
    if (!C[v]) {
      if (d[v] & 1)
        C[v] = 1;
      else
        C[v] = 2;
    }
    priority_queue<P, vector<P>, greater<P>> pq;
    for (auto &u : G2[v]) pq.push({D[u], u});
    if (!pq.empty()) {
      int u = pq.top().second;
      int e = 0;
      if (v > u)
        e = m[{u, v}];
      else
        e = m[{v, u}];
      if (E[e]) continue;
      E[e] = D[v];
    }
  }
  if (!ok) {
    puts("-1");
  } else {
    for (int i = 0; i < N; i += 1) printf("%c", (C[i] == 1) ? 'B' : 'W');
    puts("");
    for (int i = 0; i < M; i += 1) {
      if (E[i])
        printf("%d\n", E[i]);
      else
        puts("1000000000");
    }
  }
  return 0;
}
