#include <bits/stdc++.h>
const int N = 200010;
using pii = std::pair<int, int>;
int fa[N], sz[N];
std::vector<pii> e[N];
pii p[N];
bool vis[N];
int d[N];
int ans[N], color[N];
bool vis1[N];
int find(int u) { return fa[u] == u ? u : (fa[u] = find(fa[u])); }
void unite(int u, int v) {
  u = find(u), v = find(v);
  if (u == v) {
    return;
  }
  if (sz[u] < sz[v]) {
    std::swap(u, v);
  }
  sz[u] += sz[v];
  fa[v] = u;
}
void dfs(int u) {
  vis1[u] = true;
  for (auto p : e[u]) {
    int v = p.first;
    if (d[v] != d[u]) {
      continue;
    }
    if (!vis1[v]) {
      color[v] = color[u] ^ 1;
      dfs(v);
    }
  }
}
int main() {
  for (int i = 0; i < N; ++i) {
    fa[i] = i;
    sz[i] = 1;
  }
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &d[i]);
    p[i] = {d[i], i};
  }
  for (int i = 0, u, v; i < m; ++i) {
    scanf("%d%d", &u, &v);
    e[u].push_back({v, i});
    e[v].push_back({u, i});
  }
  std::sort(p + 1, p + 1 + n);
  for (int i = 1, j = 1; i <= n; i = j) {
    while (j <= n && p[j].first == p[i].first) {
      ++j;
    }
    int value = p[i].first;
    for (int k = i; k < j; ++k) {
      for (auto p : e[::p[k].second]) {
        int v = p.first;
        if (d[v] <= value) {
          unite(::p[k].second, v);
          ans[p.second] = value;
        }
      }
    }
    for (int k = i; k < j; ++k) {
      if (sz[find(p[k].second)] == 1) {
        puts("-1");
        return 0;
      }
    }
    for (int k = i; k < j; ++k) {
      int vertex = p[k].second;
      if (!vis1[vertex]) {
        bool flag = false;
        for (auto p : e[vertex]) {
          int v = p.second;
          if (d[v] < value) {
            color[vertex] = color[v] ^ 1;
            flag = true;
            break;
          }
        }
        if (flag) {
          dfs(vertex);
        }
      }
    }
    for (int k = i; k < j; ++k) {
      if (!vis1[p[k].second]) {
        dfs(p[k].second);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    putchar(color[i] ? 'B' : 'W');
  }
  puts("");
  for (int i = 0; i < m; ++i) {
    printf("%d\n", ans[i] == 0 ? int(1e9) : ans[i]);
  }
  return 0;
}
