#include <bits/stdc++.h>
int d[100005];
int order[100005];
struct Edge {
  int u, v;
  int id;
  Edge(int u, int v, int id) : u(u), v(v), id(id) {}
};
std::vector<Edge> g[100005];
int color[100005], weight[200005];
int parent[100005], edge[100005];
struct Node {
  int u;
  Node(int u) : u(u) {}
  bool operator<(const Node& other) const { return d[u] > d[other.u]; }
};
int cmp(int u, int v) { return d[u] < d[v]; }
int solve(int n) {
  int p = 1, ok = 1;
  while (p <= n) {
    int nextP = p, value = d[order[p]];
    while (nextP + 1 <= n && d[order[nextP + 1]] == value) nextP++;
    for (int i = p; i <= nextP; i++) {
      int u = order[i];
      if (parent[u] != 0) color[u] = !color[parent[u]];
    }
    for (int i = p; i <= nextP; i++) {
      int u = order[i];
      for (int j = 0; j < (int)g[u].size(); j++) {
        int v = g[u][j].v;
        if (d[v] == value && edge[u] == 0) {
          edge[u] = g[u][j].id;
          if (edge[v] == 0) edge[v] = g[u][j].id, color[v] = 1;
          color[u] = !color[v];
        } else if (edge[v] == 0) {
          edge[v] = g[u][j].id;
          parent[v] = u;
        }
      }
    }
    for (int i = p; i <= nextP; i++) {
      int u = order[i];
      if (edge[u] == 0) {
        ok = 0;
      } else
        weight[edge[u]] = value;
    }
    if (!ok)
      break;
    else
      p = nextP + 1;
  }
  return ok;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &d[i]), order[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(Edge(u, v, i));
    g[v].push_back(Edge(v, u, i));
  }
  std::sort(order + 1, order + 1 + n, cmp);
  int res = solve(n);
  if (res == 0)
    printf("-1\n");
  else {
    for (int i = 1; i <= n; i++) printf("%c", color[i] ? 'B' : 'W');
    printf("\n");
    for (int i = 1; i <= m; i++)
      printf("%d\n", weight[i] ? weight[i] : 1000000000);
  }
  return 0;
}
